文件:`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 当我运行脚本时,我得到以下内容: 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/ " 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 havingsome issues with something that looks right and does not work. I amtrying to get myself more familure with reading files. Based on thetutorials at www.python.org This "should" work. but im not sure whatthe issue is. ===SNIP===import string vsftpd=open(''vsftpd.conf'', ''r'')print vsftpdvsftpd.read()vsftpd.readlines() vsftpd.close()===SNIP=== When I check the permissions to ensure they are correct I get thefollowing. stat vsftpd.confFile: `vsftpd.conf''Size: 4137 Blocks: 16 IO Block: 131072 regularfileDevice: 802h/2050d Inode: 51010 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1000/ testing) Gid: ( 1000/testing)Access: 2005-12-19 10:21:04.000000000 +0000Modify: 2005-12-16 12:34:00.000000000 +0000Change: 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 priorto 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. regardsSteve--Steve Holden +44 150 684 7255 +1 800 494 3119Holden Web LLC www.holdenweb.comPyCon TX 2006 www.python.org/pycon/ "Johhny" <ex*****@hotmail.com> writes: ===SNIP=== import stringWhy''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 vsftpdPrint the object''s __str__. vsftpd.read()Read the full file into the string at the LHS (left side of the ''=''). In yourcase, 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> 这篇关于阅读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 16:18