我运行一个Python代码(filename- images.py),其载录如下─
import gzip
f = gzip.open('i1.gz','r')
但它显示了FileNotFoundError。我的文件夹中包含images.py看起来喜欢 -
New Folder/
images.py
i1.gz
(...Some other files...)
问题是,你是不是从内部运行的脚本New Folder
。您可以轻松地通过使用没有硬编码的绝对路径解决这个问题:
from os import path
path = path.abspath(__file__) # full path of your script
dir_path = path.dirname(path) # full path of the directory of your script
zip_file_path = path.join(dir_path,'i1.gz') # absolute zip file path
# and now you can open it
f = gzip.open(zip_file_path,'r')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句