Just a quick one- I'm writing code that asks the user for a filename and checks that it exists. If it does it has to print the file's contents, if not print a message.

import os
fileName = raw_input("Enter the name of the file: ")
if os.path.exists(fileName) == True:
    fileName.read()
else:
    print "error, that file does not exist"

As fileName is now a string, how do I read the contents of the user's input?

Thank you

Edited 6 Years Ago by jozz3: n/a

No error but no output either

import os
fileName = raw_input("Enter the name of the file: ")
if os.path.exists(fileName) == True:
   outFile = open(fileName).read()
else:
    print "error, that file does not exist"

Have tried mutiple files, all with content, and still no output

Have tried mutiple files, all with content, and still no output

content_string = open(fileName).read()
print(content_string) # <---- OUTPUT

Edited 6 Years Ago by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.