Python脚本在pycharm中运行,但不在ubuntu终端中运行



所以我有一个在PyCharm中完美运行但在Ubuntu终端中出错的脚本:

Python代码

# import dependencies #
import requests
from datetime import datetime
import os
import shutil
# make folder with today's date #
# if a folder with today's date already exists, a pass will be returned and the script will end #
today = datetime.now()
dst = "/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Company_Listings/" + today.strftime(
'%Y%m%d')
if os.path.exists(dst):
pass
else:
os.mkdir(dst)
# download company listings from the internet #
url_list = ['https://www.tsx.com/resource/en/101',
'https://asx.api.markitdigital.com/asx-research/1.0/companies/directory/file?access_token'
'=83ff96335c2d45a094df02a206a39ff4']
r_tsx = requests.get(url_list[0], allow_redirects=True)
r_asx = requests.get(url_list[1], allow_redirects=True)
open('tsx_listings.xlsx', 'wb').write(r_tsx.content)
open('asx_listings.csv', 'wb').write(r_asx.content)
# move listing files to daily folder #
src_tsx = "/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Scripts/tsx_listings.xlsx"
src_asx = "/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Scripts/asx_listings.csv"
src_list = [src_tsx, src_asx]
for item in src_list:
shutil.move(src_list[src_list.index(item)], dst)

终端错误

name@name-XPS-13-9310:~$ python3 '/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Scripts/ubuntu_testing.py' 
Traceback (most recent call last):
File "/usr/lib/python3.8/shutil.py", line 788, in move
os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: '/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Scripts/tsx_listings.xlsx' -> '/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Company_Listings/20210704/tsx_listings.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Scripts/ubuntu_testing.py", line 34, in <module>
shutil.move(src_list[src_list.index(item)], dst)
File "/usr/lib/python3.8/shutil.py", line 802, in move
copy_function(src, real_dst)
File "/usr/lib/python3.8/shutil.py", line 432, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.8/shutil.py", line 261, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: '/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Scripts/tsx_listings.xlsx'

缺少的文件/目录在脚本运行之前并不存在,但它们是由脚本创建的。我在PyCharm运行脚本的同一位置运行脚本;我还尝试过从终端使用PyCharm虚拟环境运行该脚本,但它给出了相同的错误。

基本上,我不明白为什么它在PyCharm中运行良好,而在Ubuntu终端中运行不好。

感谢

当您在终端中运行它时,应该提供绝对路径

因此,正如Pedru所暗示的,错误是使用默认路径和打开函数写入文件的结果:

open('tsx_listings.xlsx', 'wb').write(r_tsx.content)
open('asx_listings.csv', 'wb').write(r_asx.content)

Pycharm和Ubuntu终端选择的默认相对路径不同。我已经通过使用脚本中的绝对路径纠正了这一点:

# import dependencies #
import requests
from datetime import datetime
import os
# make folder with today's date #
# if a folder with today's date already exists, a pass will be returned and the script will end #
today = datetime.now()
dst = "/home/name/Dropbox/Documents/Python/Projects/MiningCompanyAnalysis/Company_Listings/" + today.strftime(
'%Y%m%d')
if os.path.exists(dst):
pass
else:
os.mkdir(dst)
# download company listings from the internet #
url_list = ['https://www.tsx.com/resource/en/101',
'https://asx.api.markitdigital.com/asx-research/1.0/companies/directory/file?access_token'
'=83ff96335c2d45a094df02a206a39ff4']
r_tsx = requests.get(url_list[0], allow_redirects=True)
r_asx = requests.get(url_list[1], allow_redirects=True)
open(f'{dst}/tsx_listings.xlsx', 'wb').write(r_tsx.content)
open(f'{dst}/asx_listings.csv', 'wb').write(r_asx.content)

这段代码现在可以在Pycharm和Ubuntu终端中运行。解决方案代码也得到了简化,因为通过将文件直接保存到所需的目标文件夹,不再需要使用shutil移动文件。

感谢Pedru和Hridaya的投入。

最新更新