Question

I am trying read the contents of file through a variable

file  = '/home/ec2-user/abc.txt'
paths  = open("file","r+")
print paths.read()

But this is not working. I need help; I don't want specify 'abc.txt' directly.

Was it helpful?

Solution

First, you need to remove the quotes around "file":

paths = open(file,"r+")

When you're calling something using the value of a variable, you shouldn't quote the variable.

Second, if you don't want to hard code the path to file, you'll need to ask the user for it:

file = raw_input("Please enter the full path to the file: ")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top