Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取用python记事本程序启动的文件名?_Python_Tkinter - Fatal编程技术网

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

如何获取用python记事本程序启动的文件名?,python,tkinter,Python,Tkinter,我想知道与我的程序一起启动的文件名(默认情况下,我将我的程序设置为文本文件,如果我知道它的路径,我可以在我的程序中打开它) p.S.sys.argv[1]是正确答案您尝试使用了吗 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

我想知道与我的程序一起启动的文件名(默认情况下,我将我的程序设置为文本文件,如果我知道它的路径,我可以在我的程序中打开它)

p.S.sys.argv[1]是正确答案

您尝试使用了吗

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__
例如,当您运行yor脚本时:

import sys
sys.argv # array of all arguments when launching 
您将拥有以下
sys.argv

python script.py hello.txt

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

哪个文件随程序启动?我不理解如果用户启动一个文本文件,它应该用我的记事本程序打开,但要这样做,我应该知道这个文本文件的路径不是
sys.argv[1]
?“#打印文件的目录”不正确。它打印当前工作目录,该目录可能是文件的位置,也可能不是文件的位置。
['script.py', 'hello.txt']