-1

The title is mostly self-explanatory. I'm trying to create a program that opens a text file based on the title a user inputs in python. However, the program doesn't print anything - it's not taking time to compute and print out the text, but instead doesn't do anything.

I've tried re-wording the program to not include the first function and simply ask the user for an input, but then I'm prompted with an error about the file not being in the proper directory. All I'm expecting is for it to print the contents of a file that the user specifies. Here is my code:

filename = 0

def get_filename():
    filename = str(input("Give a file name: "))
    return filename


def process_file():
    reading = open(filename, "r", encoding="utf-8")
    lines = reading.readlines()
    for line in lines:
        print(line)


def main():
    get_filename()
    process_file()

main()
filename.close()

Jason
  • 1
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – wovano Nov 11 '22 at 18:17

4 Answers4

2

This is the shortest possible way to do it:

with open(input("Filename: "), "r") as file:
    print(file.read())
BokiX
  • 412
  • 2
  • 14
0

Your program opens file 0, which could be stdin, so your program is waiting for input. I suspect you didn't mean that.

Your code should pass parameters around:

def get_filename():
    return input("Give a file name: ")


def process_file(filename):
    with open(filename, "r", encoding="utf-8") as file:
        for line in file:
            print(line)



def main():
    filename = get_filename()
    process_file(filename)

main()
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

The variable filename is global in some of the functions, but local in the get_filename() function. Variables are local by default, and only when you try to use their value prior to defining it in a function Python tries to fallback to the global value. To override this and say that the global "filename" variable should be used in get_filename one could add a diretive with the global keyword as the first line of the function: global filename.

That said, the best practice is to explicit pass the value around as a an argument to functions, than relying in a global variable:

def get_filename():
    filename = input("Give a file name: ")
    return filename


def process_file(filename):
    reading = open(filename, "r", encoding="utf-8")
    lines = reading.readlines()
    for line in lines:
        print(line)
    reading.close()


def main():
    filename = get_filename()
    process_file(filename)

main()


Also - there is no need to call str with the return value of input: it is already a string.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

You don't use the filename returned by get_filename(). You have to get it and pass it to process_file()

def get_filename():
    filename = str(input("Give a file name: "))
    return filename

def process_file(filename):
    reading = open(filename, "r", encoding="utf-8")
    lines = reading.readlines()
    for line in lines:
        print(line)


def main():
    filename = get_filename()
    process_file(filename)

main()
filename.close()

An alternative is - inside the functions - to change the scope of filename from local (default) to global.

def get_filename():
    global filename
    filename = str(input("Give a file name: "))

def process_file():
    global filename
    reading = open(filename, "r", encoding="utf-8")
    lines = reading.readlines()
    for line in lines:
        print(line)


def main():
    get_filename()
    process_file()

main()
filename.close()
0x0fba
  • 1,520
  • 1
  • 1
  • 11
  • What is the benefit of initializing 'filename' as a global in both functions, or at all? – Captain Caveman Nov 08 '22 at 20:51
  • Inside a funtion if you don't declare a variable as `global` any modification on that variable will stay `local`, meaning that as soon as you exit from the function, all modifications are "lost". If you dig into the concept local variables are stored on the `stack` and global ones on the `heap`. – 0x0fba Nov 08 '22 at 21:06