0

I am trying to open a file on python using this code:

fileName=input('Please enter the file name: ')
file=open(fileName,'r')

I get asked to enter the file name which is grid.txt, I type that in but nothing appears, am I doing something wrong, if so what am I doing wrong and what is the solution.

Thanks.

Rob Murray
  • 1,773
  • 6
  • 20
  • 32
  • 5
    ya, you didn't add any print stmts.. And also don't use `file` as variable name. – Avinash Raj Nov 27 '15 at 13:50
  • yeah you have to do sth. with the opened file otherwise there will nothing happen ;-) I'd also recommend to use the with statement when handling files. Otherwise you (at least at python 2.x) have to manually close the file after processing it. this would look like with open(...) as in_file: do_sth(...) – Stefan Reinhardt Nov 27 '15 at 13:53
  • @AvinashRaj: Hmm...doesn't matter in Python 3 about [use `file` as variable name](http://stackoverflow.com/questions/112970/python-when-to-use-file-vs-open). But however I'd recommend use `f` or `inf`, `outf`, etc. – Remi Guan Nov 27 '15 at 13:54
  • if nothing happened, that's good, because this doesn't do anything. `open` only creates a file object to be used for further processing, doesn't "open" the file in a window or anything like that if that's what you were expecting.. – Munir Nov 27 '15 at 13:54

5 Answers5

1

Here's some code doing what you are looking for:

fileName=input('Please enter the file name: ')
f=open(fileName,'r')
print(f.read())
f.close()
Rob Murray
  • 1,773
  • 6
  • 20
  • 32
1

You have successfully created the file object, however you have only stated that it exists.

All you need is to print it afterwards, here's an example below :

f = open('workfile', 'r')
print f.read()

Alternatively f.readline() will read the next line each time it is called, and by convention f.close() should be called to close the file after you are done reading/writing to it.

tombam95
  • 343
  • 2
  • 14
1

You can also use with

fileName = input('Please enter the file name: ')
with open(fileName, 'r') as fd:
    for line in fd:
        print(line.strip())

This will close the file when it's done as well

Steven Summers
  • 5,079
  • 2
  • 20
  • 31
0

file.open() doesn't open a file in a text editor (which is what I'm assuming you thought it does). Instead, it prepares the data for access via python.

As stated in the comment below your Question already: you have to do something with the file.

Try:

with open(fileName) as f:
    print(f.read())

Read up on the documentation for open() here. Also, using the with open() statement will improve your codes readability, as well as handling the closing of the file for you. .

deepbrook
  • 2,523
  • 4
  • 28
  • 49
0

Add following print stmt to see detail about file

with open(fileName) as file:

print("Name of the file: ", file.name)

print("Closed or not : ", file.closed)

print("Opening mode : ", file.mode)

print("Softspace flag : ", file.softspace)

print("file read:", file.read())