1

I'm trying to write a Python 2.7 function that will allow me to type in the name of text file into the pre-made GUI which will open the text file and then pass on the file name as the base for other functions (i.e. count characters) to read that file. I wrote the function before I was given the GUI and now I'm having issue with global variable.

def OpenFile(filename):
    try:
        rfile = open(filename, "r") #please type the file as a string
    except IOError:
        print ("File Not Found") 
    else:
        print ("File Opened")

This is what I wrote and I'm not sure where to go from there cause the function is supposed to open the file and prints either of the messages accordingly. I've been trying to find the solution but I'm really struggling.

2
  • Your indentation is incorrect. What is the global variable? What is the issue? Commented Oct 21, 2018 at 2:02
  • I tried to declare global variable but it wasn't working cause then I got the error saying that filename is both local and global. Also I fixed the indentation, thanks for pointing that out Commented Oct 21, 2018 at 2:17

1 Answer 1

3

The function accepts a file path input and returns the lines in the file and prints 'File Success'. The functions returns f.readlines which is basically a list of all the lines in the file.

def get(filename):
        try:
            with open(filename, "r") as f:
                print('File Success')
                return f.readlines()
        except IOError:
            print ("File Not Found") 

fname = input("Please insert file path")

contents = get(fname)

I also edited the code to have the input() outside which improves testability.

Sign up to request clarification or add additional context in comments.

5 Comments

This works great, but I'd caution that the get function is doing too many things. def get(fname): try: with open(fname) as f: ... and fname = input(""); contents = get(fname) is cleaner (and vastly more testable!)
Thank you. I'm really new to Python. To make sure that I'm on the same page as you, I tried to run the code but it doesn't work when I tried to type in the file name in input(). The box to type in the file name is already given to me so I just need to write a function that will allow me to read the name of the file then open it. I'm struggling really hard and I've been googling for the past 3 hours and got no solutions
In that case, you do not need to use input. You can remove that line and replace fname to the name of the variable given to you. I also edited my answer based on @AdamSmith 's comment, to make it more testable =)
Thank you so much for your help. I don't think I explained my initial issue clearly. This is still not I'm looking for. I still have to integrate the code with GUI given to me and it's been very challenging - not sure how to that either...I'm so sorry but I truly appreciate your help!
Why don't you post your code snippet? We are here to help but you need to be as clear as you can. The answers provided can only be as what you have asked, in this case to return your files with an input file name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.