[英]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.