使用open命令读取文件 [英] Read a file with open command
问题描述
我可以使用以下命令访问文件:
file_obj = open(" D:\ My documents \Textfile.txt",'r'')
当我现在尝试使用以下命令读取文件时:
file_obj = open(" D:\ Myy documents\File.ods" ;''r'')它不起作用。
扩展名ods来自OpenOffice.org Calc。
为什么?
jean-jeanot
只需使用file_obj =打开(D:\ Myy文件\ .File.ods,rb)用于
以二进制访问模式打开文件,这是二进制文件所必需的
MS Windows上的
。
Honza
jean-jeanot写道:
我可以使用以下命令访问文件:
file_obj = open(" D:\ My documents \Textfile.txt",'r'')
Whe我现在尝试使用以下命令读取文件:
file_obj = open(" D:\ My documents \File.ods",''r'')它不起作用。
扩展名ods来自OpenOffice.org Calc。
为什么?
jean-jeanot
jean-jeanot写道:
我可以访问使用以下命令的文件:
file_obj = open(" D:\ my documents \Textfile.txt",''r'')
在驱动器号前面有空格?我不这么认为。
在提问时,*不要*输入您认为使用的内容,
复制/粘贴您实际使用的内容。
>
当我现在尝试使用以下命令读取文件时:
file_obj = open(" D:\ My documents \ .File.ods",'r'')它不起作用。
扩展名ods来自OpenOffice.org Calc 。
为什么?
你还没有告诉我们什么是它不起作用意思是,所以我们必须玩b $ b玩猜猜游戏......可能至少有两个可能的原因:
(1).ods文件是二进制的你和你没有指定''rb''
(2)你真的键入了d:\ my documents\file.ods并且\f得到
被解释为换页符。你应该*从不*键入文字
这样的Windows文件名。相反,您有三种选择:
(a)" d:\\my documents\\ file.ods" #不推荐
(b)r" d:\ my documents\file.ods"
(c)" d:/ my documents / file.ods" ;
我建议您解决*上述两个问题并再试一次。
HTH,
John
很抱歉,访问模式不是XP Windows的二进制模式。最后,对于
读取文件,必须使用斜杠或双反斜杠。
如果文件是一个简单的文本文件,使用反斜杠可能不是
推荐但它正在运作。
无论如何,非常感谢。这是程序:
>> file_obj = open(" D:/ Mes documents / ADB Anna.ods",'r'')
s = file_obj
s .readlines()
Jean-Jeanot
Jan Svecaécrit:
只需使用file_obj = open(D:\ My documents \ File.ods,''rb'' )对于
以二进制访问模式打开文件,这是MS Windows上二进制文件所需的
。
Honza
jean-jeanot写道:
我可以访问af使用命令执行:
file_obj = open(" D:\ my documents \Textfile.txt",''r'')
当我现在尝试使用以下命令读取文件时:
file_obj = open(" D:\ My documents \ .File.ods",'r'')它不起作用。
扩展名ods是来自OpenOffice.org Calc。
为什么?
jean-jeanot
I can access to a file with the command:
file_obj = open ( " D:\My documents\Textfile.txt",''r'')
When I now try to read a file with the following command:
file_obj = open ("D:\My documents\File.ods",''r'') it doesn''t function.
The extension ods is coming from OpenOffice.org Calc.
Why ?
jean-jeanot
Hi,
simply use file_obj = open ("D:\My documents\File.ods",''rb'') for
opening file in binary access mode, which is required for binary files
on MS Windows.
Honza
jean-jeanot wrote:I can access to a file with the command:
file_obj = open ( " D:\My documents\Textfile.txt",''r'')
When I now try to read a file with the following command:
file_obj = open ("D:\My documents\File.ods",''r'') it doesn''t function.
The extension ods is coming from OpenOffice.org Calc.
Why ?
jean-jeanot
jean-jeanot wrote:I can access to a file with the command:
file_obj = open ( " D:\My documents\Textfile.txt",''r'')With a space before the drive letter? I don''t think so.
When asking questions, *don''t* type what you thought you used,
copy/paste what you actually used.
>
When I now try to read a file with the following command:
file_obj = open ("D:\My documents\File.ods",''r'') it doesn''t function.
The extension ods is coming from OpenOffice.org Calc.
Why ?You haven''t told us what "it doesn''t function" means, so we''ll have to
play guessing games ...could be for at least two possible reasons:
(1) .ods files are binary and you didn''t specify ''rb''
(2) you really typed "d:\my documents\file.ods" and the \f got
interpreted as a form-feed character. You should *never* type literal
Windows filenames like that. Instead, you have three choices:
(a) "d:\\my documents\\file.ods" # not recommended
(b) r"d:\my documents\file.ods"
(c) "d:/my documents/file.ods"
I''d suggest that you fix *both* of the above problems and try again.
HTH,
John
Sorry, but the access mode is not binary with XP Windows. Finally for
reading the file it is necessary to use a slash or a double backslash.
If the file is a simple textfile, using a backslash is perhaps not
recommended but it is functionning.
Anyway many thanks.Here is the program:
>>file_obj= open ("D:/Mes documents/ADB Anna.ods",''r'')
s = file_obj
s.readlines()
Jean-Jeanot
Jan Svec a écrit :
Hi,
simply use file_obj = open ("D:\My documents\File.ods",''rb'') for
opening file in binary access mode, which is required for binary files
on MS Windows.
Honza
jean-jeanot wrote:I can access to a file with the command:
file_obj = open ( " D:\My documents\Textfile.txt",''r'')
When I now try to read a file with the following command:
file_obj = open ("D:\My documents\File.ods",''r'') it doesn''t function.
The extension ods is coming from OpenOffice.org Calc.
Why ?
jean-jeanot
这篇关于使用open命令读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!