简体   繁体   中英

Python script runs in pycharm but not in ubuntu terminal

So I have a script that runs perfectly in PyCharm but errors out in the Ubuntu terminal:

Python Code

# 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)

Terminal Error

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'

The missing files/directories do not exist prior to the script being run, but they are created by the script. I am running the script from the same location that it is being run by PyCharm; I have also tried running the script using the PyCharm virtual environment from the terminal and it gives the same error.

Basically I don't understand why it will run fine in PyCharm and not in the Ubuntu terminal.

Thanks

在终端中运行时应该提供绝对路径

So as Pedru alluded to the error was the result of writing the files using default paths and the open function:

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

The default relative paths selected by Pycharm and the Ubuntu terminal are different. I've corrected this by using the absolute paths in the script:

    # 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)

This code now runs in both Pycharm and the Ubuntu terminal. The solution code has also been simplified as by saving the files directly to their desired destination folder, it is no longer necessary to move the files by using shutil.

Thanks to Pedru and Hridaya for the input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM