Home > Mobile >  I'm trying to allow the user to input the name of a file and then print the contents of the fil
I'm trying to allow the user to input the name of a file and then print the contents of the fil

Time:11-09

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()

CodePudding user response:

This is the shortest possible way to do it:

with open(input("Filename: "), "r") as file:
    print(file.read())

CodePudding user response:

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()

CodePudding user response:

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.

CodePudding user response:

Call process_file() from get_filename() passing the filename as an argument. Then you can guarantee the filename is stored in the variable. You don't need to declare filename as a global.

def get_filename():
    filename = str(input("Give a file name: "))
    process_file(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():
    get_filename()

main()

CodePudding user response:

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()
  • Related