簡體   English   中英

Python 腳本在 pycharm 中運行但不在 ubuntu 終端中運行

[英]Python script runs in pycharm but not in ubuntu terminal

所以我有一個在 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 函數寫入文件的結果:

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 的投入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM