简体   繁体   中英

Executing a Python scripts from Finder vs Terminal

I have a simple Python script which finds a CSV file in the directory its located in and converts the country codes to the full names, and outputs the results to a new file. This works fine when I run from my IDE or when I run manually from Terminal.

The problem comes when I try to run it either as a .command or through Python Launcher, it gives me an error:

reader = csv.DictReader((open(glob.glob("*.csv")[0])))
IndexError: list index out of range

As you can see, I'm finding the file by using glob, so it grabs all .csv files and picks the first one, however it doesn't seem to be finding any files, which leads me to think it may be running from a different location, or being run in a different way somehow.

Is there any difference between running from the command line/IDE vs running from the finder with a .command extension or Python Launcher?

Yes, it is possible. You can check the path of python using this code:

import sys
print(sys.path)

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