繁体   English   中英

如何获取使用 python 记事本程序启动的文件的名称?

[英]How can I get the name of the file launched with my python notepad program?

import os
print (os.getcwd ()) # prints directory of the file, but I should know the name too
print (__file__) # prints name of script
print (__name__) # prints __main__

我想知道使用我的程序启动的文件的名称(我将我的程序默认设置为文本文件,如果我知道它的路径,我可以在我的程序中打开它)

PS sys.argv[1] 是真正的答案

你试过用

import sys
sys.argv # array of all arguments when launching 

例如,当您运行您的脚本时:

python script.py hello.txt

您将拥有以下sys.argv

['script.py', 'hello.txt']

它取决于您的系统,但通常,在使用程序打开文件时,您会运行这种类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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