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
  Compare current date on calendar with date format file name Fioravanti 1 129 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Replace a text/word in docx file using Python Devan 4 2,864 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 874 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,121 Jul-07-2023, 05:44 PM
Last Post: flash77
  output provide the filename along with the input file processed. arjunaram 1 903 Apr-13-2023, 08:15 PM
Last Post: menator01
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,503 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Listing directories (as a text file) kiwi99 1 802 Feb-17-2023, 12:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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