0

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 by jozz3: n/a

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Gribouillis
0

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"
0

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

1

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

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

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.