Python Forum
getting current filename of a text editor file for printing file name?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting current filename of a text editor file for printing file name?
#1
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?
Reply
#2
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)           
Reply
#3
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
Reply
#4
You should close the file too, I think
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,161 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 956 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Problems writing a large text file in python Vilius 4 1,042 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,065 Nov-21-2024, 11:48 PM
Last Post: haihal
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,087 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 983 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,480 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Reading an ASCII text file and parsing data... oradba4u 2 1,464 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 4,387 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  very newbie problem on text file zapad 2 1,061 Apr-12-2024, 06:50 PM
Last Post: zapad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020