Вопрос

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.

Это было полезно?

Решение

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: ")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top