阅读文件 [英] reading files

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

问题描述

大家好,


我正在通过学​​习python作为一门语言。我有一些看起来正确但不起作用的问题。我想用读取文件让自己变得更加熟悉。基于 www.python.org 上的

教程;应该"工作。但我不确定

问题是什么。


=== SNIP ===

导入字符串


vsftpd = open(''vsftpd.conf'',''r'')

print vsftpd

vsftpd.read()< br $>
vsftpd.readlines()


vsftpd.close()

=== SNIP ===


当我检查权限以确保它们是正确的时,我得到

以下。


stat vsftpd.conf

文件:`vsftpd.conf''

大小:4137块:16 IO块:131072常规

文件

设备:802h / 2050d Inode:51010链接:1

访问:(0644 / -rw-r - r--)Uid:(1000 /测试)Gid:(1000 /

测试)

访问日期:2005-12-19 10:21:04.000000000 +0000

修改时间:2005-12-16 12:34:00.000000000 +0000
更改:2005-12-16 12:34:00.000000000 +0000


当我运行脚本时,我得到以下内容:


python reading_file.py

< open file''vsftpd.conf'',模式''r''在0xb7d742a8>


有没有人对此问题有任何建议。


问候,


Johhny。

解决方案



Johhny写道:

大家好,

我正在努力学习python作为一种语言。我有一些看起来正确但不起作用的问题。我正在尝试通过阅读文件让自己变得更加熟悉。基于 www.python.org 上的
教程,这个应该。工作。但我不确定问题是什么。

=== SNIP ===
导入字符串

vsftpd = open(''vsftpd。 conf'',''r'')
打印vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
=== SNIP ===

当我检查权限以确保它们是正确的时我得到
以下。

stat vsftpd.conf 文件:`vsftpd.conf''
大小:4137块:16 IO块:131072常规
文件
设备:802h / 2050d Inode:51010链接:1
访问: (0644 / -rw-r - r--)Uid:(1000 /测试)Gid:(1000 /
测试)
访问:2005-12-19 10:21:04.000000000 +0000 <修改:2005-12-16 12:34:00.000000000 +0000
更改时间:2005-12-16 12:34:00.000000000 +0000

当我运行脚本时,我得到以下内容:

python reading_file.py
<打开文件''vsftpd.conf'',模式''r''在0xb7d742a8>

有没有人有任何adv关于这个问题的冰。

问候,



出于好奇,你之前还有其他编程背景

to使用python?


Johhny写道:

大家好,

我正在努力学习python作为一门语言。我有一些看起来正确但不起作用的问题。我正在尝试通过阅读文件让自己变得更加熟悉。基于 www.python.org 上的
教程,这个应该。工作。但我不确定问题是什么。

=== SNIP ===
导入字符串

vsftpd = open(''vsftpd。 conf'',''r'')
打印vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
=== SNIP ===

当我检查权限以确保它们是正确的时我得到
以下。

stat vsftpd.conf 文件:`vsftpd.conf''
大小:4137块:16 IO块:131072常规
文件
设备:802h / 2050d Inode:51010链接:1
访问: (0644 / -rw-r - r--)Uid:(1000 /测试)Gid:(1000 /
测试)
访问:2005-12-19 10:21:04.000000000 +0000 <修改:2005-12-16 12:34:00.000000000 +0000
更改时间:2005-12-16 12:34:00.000000000 +0000

当我运行脚本时,我得到以下内容:

python reading_file.py
<打开文件''vsftpd.conf'',模式''r''在0xb7d742a8>

有没有人有任何adv在这个问题上冰。



是的:Python正在完全按照你所说的去做。


vsftpd是一个文件对象,你所看到的是该对象的表示

(repr())。


尝试


print vsftpd.read()


而不是你会看到文件的内容。


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


< BLOCKQUOTE>" Johhny" <前***** @ hotmail.com>写道:

=== SNIP ===
导入字符串


为什么在这里?你不需要那个导入......

vsftpd = open(''vsftpd.conf'',''r'')


开放该文件并将其生成的* object *与''vsftpd''变量相关联。

print vsftpd


打印对象'__str __。

vsftpd.read()


将完整文件读入LHS的字符串('=''的左侧)。在你的

的情况下,它会读取完整的文件并丢弃其内容。

vsftpd.readlines()


阅读所有其他行(无)进入LHS的数组。

vsftpd.close()


关闭文件。

当我运行脚本我得到以下内容:

python reading_file.py
<打开文件''vsftpd.conf'',模式''r''在0xb7d742a8>
<有没有人对这个问题有任何建议。




什么问题?你没有对你所阅读的内容做任何事情,只是对象本身。

-

Jorge Godoy< go *** @ ieee.org>

Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open(''vsftpd.conf'', ''r'')
print vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf''
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000000 +0000
Modify: 2005-12-16 12:34:00.000000000 +0000
Change: 2005-12-16 12:34:00.000000000 +0000

When I run the script I get the following:

python reading_file.py
<open file ''vsftpd.conf'', mode ''r'' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Regards,

Johhny.

解决方案


Johhny wrote:

Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open(''vsftpd.conf'', ''r'')
print vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf''
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000000 +0000
Modify: 2005-12-16 12:34:00.000000000 +0000
Change: 2005-12-16 12:34:00.000000000 +0000

When I run the script I get the following:

python reading_file.py
<open file ''vsftpd.conf'', mode ''r'' at 0xb7d742a8>

Does anyone have any advice on this issue at all.

Regards,


out of curiosity, what other programming background do you have prior
to use python ?


Johhny wrote:

Hello All,

I am working my way through learning python as a language. I am having
some issues with something that looks right and does not work. I am
trying to get myself more familure with reading files. Based on the
tutorials at www.python.org This "should" work. but im not sure what
the issue is.

===SNIP===
import string

vsftpd=open(''vsftpd.conf'', ''r'')
print vsftpd
vsftpd.read()
vsftpd.readlines()

vsftpd.close()
===SNIP===

When I check the permissions to ensure they are correct I get the
following.

stat vsftpd.conf
File: `vsftpd.conf''
Size: 4137 Blocks: 16 IO Block: 131072 regular
file
Device: 802h/2050d Inode: 51010 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/
testing)
Access: 2005-12-19 10:21:04.000000000 +0000
Modify: 2005-12-16 12:34:00.000000000 +0000
Change: 2005-12-16 12:34:00.000000000 +0000

When I run the script I get the following:

python reading_file.py
<open file ''vsftpd.conf'', mode ''r'' at 0xb7d742a8>

Does anyone have any advice on this issue at all.


Yes: Python is doing exactly what you''ve told it to.

vsftpd is a file object, and what you are seeing is the representation
(repr()) of that object.

Try

print vsftpd.read()

instead and you''ll see the content of the file.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


"Johhny" <ex*****@hotmail.com> writes:

===SNIP===
import string
Why''s that here? You don''t need that import...
vsftpd=open(''vsftpd.conf'', ''r'')
Open the file and associate its resulting *object* to the ''vsftpd'' variable.
print vsftpd
Print the object''s __str__.
vsftpd.read()
Read the full file into the string at the LHS (left side of the ''=''). In your
case, it reads the full file and discard its contents.
vsftpd.readlines()
Read all other lines (none) into an array on the LHS.
vsftpd.close()
Close the file.
When I run the script I get the following:

python reading_file.py
<open file ''vsftpd.conf'', mode ''r'' at 0xb7d742a8>

Does anyone have any advice on this issue at all.



What issue? You did nothing with what you read, just with the object itself.
--
Jorge Godoy <go***@ieee.org>


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

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