Flask app on Pythonanywhere - FileNotFoundError: [Errno 2] No such file or directory
Flask app on Pythonanywhere - FileNotFoundError: [Errno 2] No such file or directory
你知道我部署在 pythonanywhere 上的 flask 应用程序有什么问题吗?
当我尝试访问端点浏览器时 return 我的错误:
Internal Server Error The server encountered an internal error and was
unable to complete your request. Either the server is overloaded or
there is an error in the application.
错误日志:
2018-05-02 18:06:51,507: File "/home/jobad/.virtualenvs/deploy/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
2018-05-02 18:06:51,507: return self.view_functions[rule.endpoint](**req.view_args)
2018-05-02 18:06:51,507: File "/home/jobad/scrapping/app.py", line 75, in antal
2018-05-02 18:06:51,507: antal_export('Antal')
2018-05-02 18:06:51,507: File "/home/jobad/scrapping/antalpl.py", line 100, in antal_export
2018-05-02 18:06:51,507: with open( 'export/%s.csv' %company_name, 'w', newline='', encoding='utf-8' ) as csvfile:
2018-05-02 18:06:51,508: FileNotFoundError: [Errno 2] No such file or directory: 'export/Antal.csv'
函数代码在这里:
def antal_export(company_name):
global a
test = []
for document in a:
event_obj = {}
event_obj['company_name'] = document['company_name']
event_obj['category'] = document['category']
event_obj['offers'] = document['offers']
test.append( event_obj )
try:
os.remove( 'export/%s.csv' %company_name )
with open( 'export/%s.csv' %company_name, 'w', newline='', encoding='utf-8') as csvfile:
fields = ['category', 'offerts']
writer = csv.DictWriter( csvfile, fieldnames=fields, delimiter=';' )
writer.writeheader()
writer.writerows( test )
except:
with open( 'export/%s.csv' %company_name, 'w', newline='', encoding='utf-8' ) as csvfile:
fields = ['company_name', 'category', 'offers']
writer = csv.DictWriter( csvfile, fieldnames=fields, delimiter=';' )
writer.writeheader()
writer.writerows( test )
a = []
和文件夹树:
(deploy) 18:06 ~/scrapping (master)$ ls
Procfile antal2.py experispl.py graftonpl.py infopracapl.py pracapl.py requirements.txt
__pycache__ antalpl.py export hayspl.py manpower.py pracujpl.py static
all.py app.py goldenlinepl.py hrkpl.py michaelpagepl.py randstadpl.py templates
在 heroku 上一切正常,但我无法使用它 - 它是一个抓取应用程序,有些请求超过 30 秒。
使用 PythonAnyWhere,确保提供 .csv
文件的完整路径(您需要为正在使用的任何文件指定完整路径,例如 .db
或 .txt
文件)。根据您提供的回溯和文件夹树,它应该是:
path = '/home/jobad/scrapping/export/{}.csv'.format(company_name)
这又可用于您需要该文件的所有操作:
os.remove(path)
with open(path, 'w', newline='', encoding='utf-8') as csvfile:
...
你知道我部署在 pythonanywhere 上的 flask 应用程序有什么问题吗? 当我尝试访问端点浏览器时 return 我的错误:
Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
错误日志:
2018-05-02 18:06:51,507: File "/home/jobad/.virtualenvs/deploy/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
2018-05-02 18:06:51,507: return self.view_functions[rule.endpoint](**req.view_args)
2018-05-02 18:06:51,507: File "/home/jobad/scrapping/app.py", line 75, in antal
2018-05-02 18:06:51,507: antal_export('Antal')
2018-05-02 18:06:51,507: File "/home/jobad/scrapping/antalpl.py", line 100, in antal_export
2018-05-02 18:06:51,507: with open( 'export/%s.csv' %company_name, 'w', newline='', encoding='utf-8' ) as csvfile:
2018-05-02 18:06:51,508: FileNotFoundError: [Errno 2] No such file or directory: 'export/Antal.csv'
函数代码在这里:
def antal_export(company_name):
global a
test = []
for document in a:
event_obj = {}
event_obj['company_name'] = document['company_name']
event_obj['category'] = document['category']
event_obj['offers'] = document['offers']
test.append( event_obj )
try:
os.remove( 'export/%s.csv' %company_name )
with open( 'export/%s.csv' %company_name, 'w', newline='', encoding='utf-8') as csvfile:
fields = ['category', 'offerts']
writer = csv.DictWriter( csvfile, fieldnames=fields, delimiter=';' )
writer.writeheader()
writer.writerows( test )
except:
with open( 'export/%s.csv' %company_name, 'w', newline='', encoding='utf-8' ) as csvfile:
fields = ['company_name', 'category', 'offers']
writer = csv.DictWriter( csvfile, fieldnames=fields, delimiter=';' )
writer.writeheader()
writer.writerows( test )
a = []
和文件夹树:
(deploy) 18:06 ~/scrapping (master)$ ls
Procfile antal2.py experispl.py graftonpl.py infopracapl.py pracapl.py requirements.txt
__pycache__ antalpl.py export hayspl.py manpower.py pracujpl.py static
all.py app.py goldenlinepl.py hrkpl.py michaelpagepl.py randstadpl.py templates
在 heroku 上一切正常,但我无法使用它 - 它是一个抓取应用程序,有些请求超过 30 秒。
使用 PythonAnyWhere,确保提供 .csv
文件的完整路径(您需要为正在使用的任何文件指定完整路径,例如 .db
或 .txt
文件)。根据您提供的回溯和文件夹树,它应该是:
path = '/home/jobad/scrapping/export/{}.csv'.format(company_name)
这又可用于您需要该文件的所有操作:
os.remove(path)
with open(path, 'w', newline='', encoding='utf-8') as csvfile:
...