Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从变量读取文件_Python_Linux_Python 2.7_Scripting - Fatal编程技术网

在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: ")