在open函数中使用相对路径...总是提示文件不存在。于是便使用绝对路径了。
获取绝对路径的代码:
python
script_dir = os.path.dirname(os.path.abspath(__file__))
self.parent_dir = os.path.dirname(script_dir)
python这个坑,挺害人的呀。记录下来,浪费了好多的时间。
Flask处理文件上传:
python
@app.route('/upload', methods = ['POST'])
def save_file():
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
if file:
file.save(os.path.join("upload_folder", file.filename))
return 'File ' + file.filename + ' successfully uploaded'
windows/linux文件锁:
python
os_name = platform.system()
if os_name == "Windows":
import msvcrt
# 锁定文件
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 0) # 锁定文件
# 写入数据
pickle.dump(global_resource, f)
# 解锁文件
msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 0) # 解锁文件
else:
import fcntl
fcntl.flock(f, fcntl.LOCK_EX)
pickle.dump(global_resource, f)
fcntl.flock(f, fcntl.LOCK_UN)