我似乎无法访问我使用的"open-with“on的文件路径
我正在创建我自己的文本编辑器,它可以通过打开选项卡(工作)打开文件,也可以使用从脚本转换而来的.exe文件打开.txt文件
我尝试了许多不同的方法来访问该文件,但似乎都不起作用
try:
fileName = str(os.path.realpath(__file__))
with open(fileName) as _file:
contentText.insert(1.0, _file.read())
onContentChanged()
root.title(os.path.basename(fileName) + " - Footprint Editor")
except:
root.title("Untitled - Footprint Editor")当.py文件转换为.exe时,我尝试使fileName与我使用的“打开”文件的路径相等
发布于 2019-08-21 18:49:58
使用_file.name获取您打开的文件的路径
with open(fileName) as _file:
print(f.name)但是,如果您想在with open之外查看路径,则可能必须使用open而不是with open
_file=open(fileName)
# Do some stuff if you want here
print(_file.name)https://stackoverflow.com/questions/57578609
复制相似问题