文件处理问题Python I / O. [英] File Handling Problems Python I/O

查看:72
本文介绍了文件处理问题Python I / O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我遇到了Python的问题。我是Python的新手编程

语言,但我确实有其他语言的经验。我是

遇到文件处理的奇怪问题,并想知道是否有人

其他已经看到这个或知道我做错了什么。我只是尝试打开一个文件进行读取并迭代直到文件结束。我是

能够毫无问题地这样做,但这里有一个问题:开放的

语句仅适用于某些文件。我打开一个简单的文本文件

说file1.txt没有任何问题,但是我将open语句更改为

另一个文本文件并且错误'出来说明文件不存在我知道代码是正确的,因为它适用于其他文件。我有

尝试二进制和ascii模式无济于事。知道为什么这会发生什么?b $ b?我在Win2k上运行Python 2.3.4 wxPython 2.5.3.1和SPE作为

IDE。谢谢


Josh

Hi,

I am having a problem with Python. I am new to Python as a programming
language, but I do have experience in other languages. I am
experiencing strange problems with File handling and wonder if anyone
else has seen this or knows what I am doing wrong. I am simply trying
to open a file for read and iterate through until the end of file. I am
able to do so without a problem, but here''s the catch: The open
statement is only working on certain files. I open a simple text file
say file1.txt without any issues, but I change the open statement to
another text file and it error''s out stating the file doesn''t exist. I
know the code is correct because it worked for the other file. I have
tried both binary and ascii modes to no avail. Any idea why this is
happening? I am running Python 2.3.4 wxPython 2.5.3.1 and SPE as the
IDE on Win2k. Thanks

Josh

推荐答案

我认为如果你不能帮助我们不要发布你的一些代码。


问候,

Arjen


Josh写道:
I don''t think we can help if you don''t post some of your code.

Regards,
Arjen

Josh wrote:


我遇到了Python的问题。我是Python的新手编程语言,但我确实有其他语言的经验。我在文件处理方面遇到了奇怪的问题,并想知道是否有其他人看到过这个或者知道我做错了什么。我只是尝试打开一个文件进行读取并迭代直到文件结束。我没有问题就能够这样做,但是这里有一个问题:开放的
语句只适用于某些文件。我打开一个简单的文本文件
说file1.txt没有任何问题,但我将open语句更改为
另一个文本文件,并且错误'出来说明该文件不存在。我知道代码是正确的,因为它适用于其他文件。我已经尝试了二进制和ascii模式无济于事。知道为什么会这样吗?我在Win2k上运行Python 2.3.4 wxPython 2.5.3.1和SPE作为
IDE。谢谢

Josh
Hi,

I am having a problem with Python. I am new to Python as a programming
language, but I do have experience in other languages. I am
experiencing strange problems with File handling and wonder if anyone
else has seen this or knows what I am doing wrong. I am simply trying
to open a file for read and iterate through until the end of file. I am
able to do so without a problem, but here''s the catch: The open
statement is only working on certain files. I open a simple text file
say file1.txt without any issues, but I change the open statement to
another text file and it error''s out stating the file doesn''t exist. I
know the code is correct because it worked for the other file. I have
tried both binary and ascii modes to no avail. Any idea why this is
happening? I am running Python 2.3.4 wxPython 2.5.3.1 and SPE as the
IDE on Win2k. Thanks

Josh



Josh写道:
我遇到了Python的问题。我是Python的新手编程语言,但我确实有其他语言的经验。我在文件处理方面遇到了奇怪的问题,并想知道是否有其他人看到过这个或者知道我做错了什么。我只是尝试打开一个文件进行读取并迭代直到文件结束。我没有问题就能够这样做,但是这里有一个问题:开放的
语句只适用于某些文件。我打开一个简单的文本文件
说file1.txt没有任何问题,但我将open语句更改为
另一个文本文件,并且错误'出来说明该文件不存在。
I am having a problem with Python. I am new to Python as a programming
language, but I do have experience in other languages. I am
experiencing strange problems with File handling and wonder if anyone
else has seen this or knows what I am doing wrong. I am simply trying
to open a file for read and iterate through until the end of file. I am
able to do so without a problem, but here''s the catch: The open
statement is only working on certain files. I open a simple text file
say file1.txt without any issues, but I change the open statement to
another text file and it error''s out stating the file doesn''t exist.




这里通常的新手错误是无法识别

字符串中的反斜杠被解释为特殊的

转义序列如果它们在某些字符之前,例如t,则为b。或b。你可能有一条看似

的路径:" c:\ temp \ myfile.txt",对吗? \t实际上是将
转换为TAB字符。


在大多数情况下,你可以在Windows中使用* forward * slash,

除了在命令行上。最简单的解决方法是将你的反斜杠转换为正斜杠:

" c:/temp/myfile.txt"


另一种不太理想的方法是使用

raw。换句话说,以防止转义序列被识别为



r" c:\ temp \ myfile.txt"


最后,并且至少可取,使用双反斜杠

来逃避每个反斜杠并实际取消通常的

转义处理:

" c:\\ temp \\myfile.txt"


这种方式的可读性差得多,但有时也是正确的事情...


-Peter



The usual rookie mistake here is to fail to recognize
that backslashes in strings are interpreted as special
escape sequences if they precede certain characters such
as "t" or "b". You probably have a path that looks like
this: "c:\temp\myfile.txt", right? The \t is actually
being converted into a TAB character.

You can use *forward* slashes in most cases in Windows,
except on the command line. The simplest fix is just
to convert your backslashes to forward slashes:
"c:/temp/myfile.txt"

Another approach, somewhat less desirable, is to use
"raw" strings instead, to prevent the escape sequences from
being recognized:
r"c:\temp\myfile.txt"

Finally, and by far least desirable, use double-backslashes
to escape each backslash and in effect nullify the usual
escape handling:
"c:\\temp\\myfile.txt"

Much less readable that way, but sometimes the right thing to do...

-Peter


他是我打电话的功能。如果我将打开的

语句更改为另一个文件,请说c:\ test.txt,我知道的文件存在,那么

会出错,说明文件不存在。谢谢


Josh


def GetStartVars(个体经营):

尝试:

DOWNFILE = open(" c:\ fixes.txt"," r")

除了IOError:

print"无法打开文件。

else:

counter = 1

而1:

theline = DOWNFILE.readline()

如果不是该行:

休息

打印该行

counter = counter + 1

打印计数器

DOWNFILE.close()

He is the function where I am making the call. If I change the open
statment to another file, say "c:\test.txt", a file I know exists, it
will error out stating the file does not exist. Thanks

Josh

def GetStartVars(self):
try:
DOWNFILE = open("c:\fixes.txt","r")
except IOError:
print "Failed to open file."
else:
counter = 1
while 1:
theline = DOWNFILE.readline()
if not theline:
break
print theline
counter = counter + 1
print counter
DOWNFILE.close()


这篇关于文件处理问题Python I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆