#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Choose read file from Terminal?


    Hey out there.
    Im kinda a new to programming, so this question is probably pretty easy for you

    I have made a program that reads from a .txt file and then i do something with the text in that file.

    I start my python with this code:
    Code:
    f = open('something.txt','r') #opens a .txt file
    page = f.read()
    so i can access the file i want. That code is followed by some functions in which i do something with the text file.

    My question is this:
    I think it would be easier if i didnt have to open my program and change the name of the text file every time i needed to do so, which is a lot. So how can i do that?
    I'm thinking it would be very handy if i could, enter the name of the text file at the same time i ran the program from the Terminal (ubuntu).
    Does anyone know a way to do this? Or maybe something even smarter?

    Cheers
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    GUI Time


    You could use a Tkinter GUI. http://www.pythonware.com/library/tkinter/introduction/


    Code:
    import pdb
    import fileinput
    import os
    from Tkinter import *
    from tkFileDialog import askopenfilename
    import time
    import io
    
    class App(Frame) :
    
      def __init__(self,master) :
        Frame.__init__(self,master)
        self.root=None
        self.grid()
        self.create_widgets()
    
      def create_widgets(self) :
    
        os.chdir(r'U:\Private\Python\Dumps')
        
        Label(self, text='                        ').grid(row=1, column=2, sticky=E+W)
    
        self.printButton=Button(self, text='Choose Text File', fg='black', bg='white', command=self.TestResultsFile)
        self.printButton.grid(row=4, column=1, columnspan=2, sticky=E+W)  
        
        for i in [5,6,7,8,9]:
          Label(self, text='').grid(row=i)
         
        self.runButton=Button(self, text='      Run       ', fg='black', bg='green',  command=self.doit)
        self.runButton.grid(row=11, column=1, sticky=E+W)  
        
        self.quitButton=Button(self, text='Quit', fg='white', bg='red', command=self.quit)
        self.quitButton.grid(row=11, column=2, columnspan=1, sticky=E+W)
    
        
       
    ##############################  Get Files  ################################################
      def TestResultsFile(self) :
        global FirstDir
        FirstDir = askopenfilename(filetypes=[('txt files','*.*')])
    
    ##############################  Main Function  ################################################
    
    
      def doit(self) :
       
        print "Done"
    
    ##############################################################################
        
    root=Tk()
    root.geometry('160x200')
    root.title('Tool')
    app=App(root)
    root.mainloop()

    Comments on this post

    • b49P23TIvg agrees : Didn't know askopenfilename existed, or of filetypes arg. Would have used it in recent project.
    Last edited by WynnDeezl; January 23rd, 2013 at 02:24 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by SoronSR
    Hey out there.
    Im kinda a new to programming, so this question is probably pretty easy for you

    I have made a program that reads from a .txt file and then i do something with the text in that file.

    I start my python with this code:
    Code:
    f = open('something.txt','r') #opens a .txt file
    page = f.read()
    so i can access the file i want. That code is followed by some functions in which i do something with the text file.

    My question is this:
    I think it would be easier if i didnt have to open my program and change the name of the text file every time i needed to do so, which is a lot. So how can i do that?
    I'm thinking it would be very handy if i could, enter the name of the text file at the same time i ran the program from the Terminal (ubuntu).
    Does anyone know a way to do this? Or maybe something even smarter?

    Cheers
    I found a solution myself. I choose to use a raw_input together with the read function.
    Code:
    pageName = raw_input("Enter filename: ") 
    f = open(pageName,'r') #opens a .txt file 
    page = f.read()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    I lean toward wynndeezl's gui solution more and more.
    Sometimes the tkinter yes/no message box is easier.

    Anyway, you want access to sys.argv . You can put the argument on the command line. For examples:
    Code:
    $ python p.py p.py  # my program is named p.py
    For your information, sys.argv holds: ['p.py', 'p.py']
    $ python p.py blort   # no such file as blort
    For your information, sys.argv holds: ['p.py', 'blort']
    Enter filename: p.py
    $ python p.py   # code requests file name
    For your information, sys.argv holds: ['p.py']
    Enter filename: p.py
    $
    Code:
    import os
    import sys
    
    print('For your information, sys.argv holds: {}'.format(sys.argv))
    
    if (1 < len(sys.argv)) and os.path.isfile(sys.argv[1]):
        pageName = sys.argv[1]
    else:
        pageName = raw_input("Enter filename: ")
    
    f = open(pageName,'r') #opens a .txt file
    page = f.read()
    Last edited by b49P23TIvg; January 23rd, 2013 at 07:13 PM. Reason: clarification
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo