def upload_file(request):
try:
if request.method == "post":
data = request.files['data']
assert data, '引數必傳 data'
num = random.randint(0, 100)
file_name = os.path.join(settings.base_dir, 'media/resume{}'.format(num))
try:
obj = open(file_name, 'wb+')
for chunk in obj.chunks(): # 將檔案寫入磁碟
return jsonresponse()
# 1.03 讀取demo
def read_img(request):
""": 讀取
return httpresponse(str(e))
# 讀取整個檔案
with open('pi_digits.txt') as f: # 預設模式為『r』,唯讀模式
contents = f.read() # 讀取檔案全部內容
# 逐行讀取
with open('pi_digits.txt') as f:
for line1 in f:
print line1 # 每行末尾會有乙個換行符
print '------------'
for line2 in f:
print line2.rstrip() # 此時檔案已經讀完,line2指向文字末尾,因此不會有輸出
Python之檔案讀取和寫入
python之檔案管理 1.檔案讀取 匯入模組 import codecs 開啟檔案例項 usr bin env python coding utf8 time 2017 10 27 9 57 author hantong file file.py import codecs f codecs.op...
python 檔案的讀取和寫入
檔案讀取 with open 當前目錄檔名或指定目錄檔案 as file object contents file object.read 讀取檔案返回整個檔案 lines file object.readlines 讀取檔案返回檔案行列表 for line in lines print line....
python(檔案讀取寫入)
python讀寫檔案 1.open 使用open開啟檔案後一定要記得呼叫檔案物件的close 方法。比如可以用try finally語句來確保最後能關閉檔案。file object open thefile.txt try all the text file object.read finally ...