在Python中从变量读取文件
我试图通过变量读取文件的内容在Python中从变量读取文件,python,linux,python-2.7,scripting,Python,Linux,Python 2.7,Scripting,我试图通过变量读取文件的内容 file = '/home/ec2-user/abc.txt' paths = open("file","r+") print paths.read() 但这是行不通的。我需要帮助;我不想直接指定'abc.txt'。首先,您需要删除“file”周围的引号: 当您使用变量的值调用某个对象时,不应引用该变量 其次,如果不想硬编码文件的路径,则需要向用户索取: file = raw_input("Please enter the full path to the f
file = '/home/ec2-user/abc.txt'
paths = open("file","r+")
print paths.read()
但这是行不通的。我需要帮助;我不想直接指定
'abc.txt'
。首先,您需要删除“file”
周围的引号:
当您使用变量的值调用某个对象时,不应引用该变量
其次,如果不想硬编码文件的路径
,则需要向用户索取:
file = raw_input("Please enter the full path to the file: ")
“文件”
是字符串,而不是变量引用。想一想。
file = raw_input("Please enter the full path to the file: ")