简体   繁体   中英

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__

I want to know the name of the file, launched with my program (I set my program by default for text files and I could open it in my program if I will know its path)

PS sys.argv[1] is the true answer

Did you tried to use

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

For example when you run yor script:

python script.py hello.txt

You will have the folowing sys.argv :

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

It depens of your system but generally, when opening a file with a program you have this type of run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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