0
업로드 된 파일을 받아들이고 유효성 검사를 위해 파일 개체를 던지는 플라스크 응용 프로그램에서 다음 경로를 사용합니다.python flask : 모방 파일 저장 개체
def is_file_valid(file):
if file.filename == 'test':
return True
return False
@app.route('/validate', method=['POST'])
def validate():
file = request.files['file']
if is_file_valid(file):
return redirect(url_for('somewhere'))
return redirect(url_for('somewhere_else'))
나는 is_file_valid
기능을 테스트하기 위해 유닛 테스트를 만들려고 해요하지만 문제는 표준 파이썬 파일 객체와 매우 유사하게 나타납니다 FileStorage
객체 플라스크 용도를 생성하는 데 문제가 있습니다 (: 여기에 베어 예제 docs).
import io
with io.open('/path/to/file', 'rb') as f:
print(f.filename)
그러나 나는 다음과 같은 오류를 받고 있어요 : 여기
는 지금까지 시도했습니다 무엇AttributeError: 'io.TextIOWrapper' object has no attribute 'filename'
나는 일반 파이썬 스크립트에서 WERKZEUG의 FileStorage
객체를 모방 할 수있는 방법 어떤 생각 ?
https://stackoverflow.com/questions/18249949/python-file-object-to-flasks-filestorage –