基本文件操作问题 [英] Basic file operation questions

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

问题描述




我是python的初学者,这是我的第一个问题:

如何使用循环读取文件的内容或者其他的东西?我用file = open(文件名,''r'')打开文件

然后该怎么办?我可以在文件中使用

类似


的xxx:

....

感谢您的帮助

Alex

解决方案

您好Alex


假设你有一个名为data.txt的文件:


***

f = open(''data.txt'','' r'')

lines = f.readlines()

f.close()

for line in lines:

打印行

***

将打印文件的每一行。


您可以进行巨额投资通过设置2或3个小时来通过

Python教程,它作为文档的一部分安装。

该教程可以为您提供大量的知识需要用

Python。


thx

Caleb

2005年2月2日13:27 :49 -0800,alex< al ************* @ mpi-hd.mpg.de>写道:



我是python的初学者,这是我的第一个问题:
如何读取文件的内容使用循环或什么?我用file = open(filename,''r'')打开文件然后该怎么办?我可以在文件中使用

这样的内容:
....

感谢您的帮助
Alex




alex写道:



我是python的初学者,这是我的第一个问题:
如何使用循环或其他东西读取文件的内容?我用file = open(filename,''r'')打开文件然后该怎么办?我可以在文件中使用

这样的内容:
....

感谢您的帮助
Alex



看看这个:
http://www.devshed.com/c/a/Python/Fi...ent-in-Python/


HTH,

- Marcel


alex写道:



我是python的初学者,这是我的第一个问题:
如何使用循环或其他内容读取文件的内容?我用file = open(filename,''r'')打开文件然后该怎么办?我可以在文件中使用

这样的文件:
....



是的,确实可以。这绝不是*所有*你可以做的,而是在文件的行上迭代

。请注意,

行仍然会终止" \ n"最后,这就是为什么以下示例中的

打印语句以逗号结尾(这会停止

打印出来自推出自己的换行符)。
< blockquote class =post_quotes>

f = file(" test92.py",''r'')
for f in f:
...打印l,

...

导入os.path


def getHomeDir():

''''''尝试查找用户的主目录,否则返回当前

目录。''''

''

试试:

path1 = os.path.expanduser("〜")

除外:

path1 =""

尝试:

path2 = os.environ [" HOME"]

除了:

path2 =""

尝试:

path3 = os.environ [" USERPROFILE"]

除了:

path3 =""


如果不是os.path.exists(path1):

如果不是os.path.exists (PA th2):

如果不是os.path.exists(path3):

返回os.getcwd()

else:return path3

else:return path2

else:return path1


print getHomeDir()




问候

Steve

-

与Python开发人员和你的clpy收藏三月见面23-25

来到PyCon DC 2005 http:/ /www.python.org/pycon/2005/

Steve Holden http://www.holdenweb.com/


Hi,

I am a beginner with python and here is my first question:
How can I read the contents of a file using a loop or something? I open
the file with file=open(filename, ''r'') and what to do then? Can I use
something like

for xxx in file:
....
Thanks for help
Alex

解决方案

Hi Alex

Assuming you have a file called "data.txt":

***
f = open(''data.txt'',''r'')
lines = f.readlines()
f.close()
for line in lines:
print line
***
Will print each line of the file.

You can make a huge investment by setting 2 or 3 hours aside to go through
the Python tutorial, which gets installed as part of the documentation.
That tutorial can get you much of the knowledge you will ever need with
Python.

thx
Caleb
On 2 Feb 2005 13:27:49 -0800, alex <al*************@mpi-hd.mpg.de> wrote:

Hi,

I am a beginner with python and here is my first question:
How can I read the contents of a file using a loop or something? I open
the file with file=open(filename, ''r'') and what to do then? Can I use
something like

for xxx in file:
....
Thanks for help
Alex




alex wrote:

Hi,

I am a beginner with python and here is my first question:
How can I read the contents of a file using a loop or something? I open
the file with file=open(filename, ''r'') and what to do then? Can I use
something like

for xxx in file:
....
Thanks for help
Alex


take a look at this:
http://www.devshed.com/c/a/Python/Fi...ent-in-Python/

HTH,
-- Marcel


alex wrote:

Hi,

I am a beginner with python and here is my first question:
How can I read the contents of a file using a loop or something? I open
the file with file=open(filename, ''r'') and what to do then? Can I use
something like

for xxx in file:
....


Yes, indeed you can. That''s by no means *all* you can do, but to iterate
over the lines of the file that will wrok exactly. Note that the lines
will still have their terminating "\n" on the end, which is why the
print statement inthe following example ends in a comma (this stops
print from putting out its own newline).

f = file("test92.py", ''r'')
for l in f: ... print l,
...
import os.path

def getHomeDir():
'''''' Try to find user''s home directory, otherwise return current
directory.''''
''
try:
path1=os.path.expanduser("~")
except:
path1=""
try:
path2=os.environ["HOME"]
except:
path2=""
try:
path3=os.environ["USERPROFILE"]
except:
path3=""

if not os.path.exists(path1):
if not os.path.exists(path2):
if not os.path.exists(path3):
return os.getcwd()
else: return path3
else: return path2
else: return path1

print getHomeDir()



regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.python.org/pycon/2005/
Steve Holden http://www.holdenweb.com/


这篇关于基本文件操作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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