Python Forum

Full Version: getting current filename of a text editor file for printing file name?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from sys import *

def open_file(filename)
      print(filename)

def run():
     open_file(?)
what should i write into the question mark to print the filename of the saved and currently opened file in the text editor i am using?
So this is what I would do if I got your question aright:

from sys import *

def open_file(filename)
      print(filename)
 
def run():
     filename = "AnyFileName" #You maybe want to convert it into a user input via input()
     open_file(filename)           
You should run like

def open_file(filename):
    print('filename: {}'.format(filename))
    # If you upgrade to python 3.6:
    # f"filename {filename}"
    return open(filename, 'r') # for read
    # return open(filename, 'w') # for write
    # Or any other mode (there are many see docs)

def run(filename):
    open_file(filename)

if __name__ == '__main__':
    run('ziggy.txt')
You may want to use the tkinter.filedialog see: http://infohost.nmt.edu/tcc/help/pubs/tk...ialog.html
You should close the file too, I think