快捷搜索:
来自 计算机编程 2019-11-16 13:30 的文章
当前位置: 67677新澳门手机版 > 计算机编程 > 正文

Python文件和迭代

文件

文件和文件夹

文本:文本文件、二进制文件

文件夹:(windows) G:pythonWorkspacepythonstudy   

    (linux/mac) /home/workspace/python

    注意:文件夹路线的斜杠linux与windows区别

windows下文件路线:示例 

1 >>> p1="G:pythonWorkspacepythonstudytest.txt"
2 >>> p2 =r"G:pythonWorkspacepythonstudytest.txt"
3 >>> p3 ="G:\pythonWorkspace\python\study\test.txt"

 

跨平台路径:os.path.abspath(path)

67677新澳门手机版 ,查看属性:os.stat(filename)

p2 =r"G:pythonWorkspacepythonstudytest.txt"

1 >>> import os #引入os 模块

2 >>> os.stat(p2) #查看文件属性 3 nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=14L, st_atime=1520953379L, st_mtime=1520953401L, st_ctime=1520953379L) 4 >>>

 

读、写文件

python 中文件也是意气风发种类型的指标,有属性 __iter__,表达是可迭代的

开垦八个文件

>>> dir(file)  #查看文件的属性
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>> f =open(p2) #打开一个文件
>>> for line in f:  #循环读取文件内容
...     print line #打印出来后,两行之间有空行,原因print 会自动的带上一个换行符
...
study python

aaaaa

>>> f =open(p2) #第二次也要open文件
>>> for line in f:
...     print line, #出现空行解决方式在line后面加,
...
study python
aaaaa
>>>

写文件

w 方式张开二个文件只用于写入。倘若该公文已存在则将其遮住。假如该文件空头支票,创设新文件

a 形式 张开贰个文书用于追加。要是该文件已存在,文件指针将会放在文件的最后。也正是说,新的剧情将会被写入到原来就有内容之后。假诺该公文不设有,成立新文件进行写入。

with 语句 能够不写close() 

1 >>> nf =open("G:\pythonWorkspace\python\study\test.txt","w") #打开一个文件,用w模式,写入
2 >>> nf.write("This is a new file.") #将这句话写入文件
3 >>> nf.close() #打开一个文件,写入后必须关闭掉
4 >>> nf =open("G:\pythonWorkspace\python\study\test.txt")
5 >>> for line in nf:
6 ...     print line
7 ...
8 This is a new file.

 1 >>> with open("G:\pythonWorkspace\python\study\test.txt","a") as fp:  #with open 也可以创建文件 打开文件,适用with open write后面的close()可以不写,它会自动处理
 2 ...     fp.write("n what's your name?")
 3 ...
 4 
 5 >>> f =open("G:\pythonWorkspace\python\study\test.txt")
 6 >>> for line in f:
 7 ...     print line
 8 ...
 9 
10 
11  what's your name?
12 >>>

分歧的读文件措施

 1 >>> help(file.read)
 2 Help on method_descriptor:
  #参数size可选,如果没有参数,将文件的全部内容读取,如果有参数,将读取到指定的字节,然后将读取到的这些内容以字符串形式返回
 3 所读取的内容一次返回到内存中,随时可以取用,方便快捷。这种方式,如果文件特别大的时候,会使内存开销太大。
 4 read(...)
 5     read([size]) -> read at most size bytes, returned as a string. 
 6 
 7     If the size argument is negative or omitted, read until EOF is reached. 
 8     Notice that when in non-blocking mode, less data than what was requested
 9     may be returned, even if no size parameter was given.
10 
11 >>> help(file.readline)
12 Help on method_descriptor: 
13 参数(size)可选,它是以行为单位返回一个字符串,每次读取一行,依次循环往下读取,如果没有参数,则将读取到文件最后,返回空字符串,到达文件末尾。END OF FILE (EOF)
14 readline(...)
15     readline([size]) -> next line from the file, as a string.
16 
17     Retain newline.  A non-negative size argument limits the maximum
18     number of bytes to return (an incomplete line may be returned then).
19     Return an empty string at EOF.
20 
21 >>> help(file.readlines)
22 Help on method_descriptor:
23 返回以行为单位的列表,相当于执行readline, 得到每一行,然后把这一行的字符串,作为列表中的元素,再放到一个列表中,最后将列表返回。
24 readlines(...)
25     readlines([size]) -> list of strings, each a line from the file.
26 
27     Call readline() repeatedly and return a list of the lines so read.
28     The optional size argument, if given, is an approximate bound on the
29     total number of bytes in the lines returned.

演示:读取文件

  read()      读取文件内容,借使指定参数,将点名字节相应的内容再次回到,若无一点点名参数,将文件内容总体赶回。

  readline() 以行为单位读取文件,重回三个字符串,每一次读取生龙活虎行,二遍巡回往下读取。若无一点名参数,将读取到文件末尾。

  readlines() 重返以行为单位的列表,相当于实践readline,获得每风姿浪漫行,然后把那大器晚成行的字符串,作为列表的因素,最终将那些列表重返

  import fileinput 读取大文件时,使用 

  f.seek(0) 改动最近文件的岗位

  f.tell() 告诉文件当前的指针地方,文件内的当前任务

 1 >>> f =open("G:\pythonWorkspace\python\study\test.txt")
 2 >>> c=f.read() #把文件的全部内容读取出来,放到一个变量
 3 >>> c
 4 "n what's your name?"
 5 >>> f =open("G:\pythonWorkspace\python\study\test.txt")
 6 >>> f.read(5) #有参数,将返回相应字节的内容
 7 'n wha'
 8 >>> f =open("G:\pythonWorkspace\python\study\test.txt")
 9 >>> f.readline() 返回第一行的内容,每一行都是一个字符串
10 'n'
11 >>> f =open("G:\pythonWorkspace\python\study\test.txt")
12 >>> f.readlines()  返回一个列表,列表中每一行为一个元素
13 ['n', " what's your name?"]
14 >>> import fileinput #引入大文件模块,避免文件太大,内存过满的问题
15 >>> for line in fileinput.input("G:\pythonWorkspace\python\study\bigfile.txt"):
16 ...     print line
17 ...
18 Before getting started,
19 
20 you may want to find out which IDEs and text editors are tailored to make Python editing easy,
21 
22 browse the list of introductory books,
23 
24 or look at code samples that you might find helpful.
25 
26 There is a list of tutorials suitable for experienced programmers on the BeginnersGuide/Tutorials page.
27 
28 There is also a list of resources in other languages which might be useful if English is not your first language.
29 >>> f=open("G:\pythonWorkspace\python\study\bigfile2.txt")
30 >>> for line in f:
31 ...     print line
32 ...
33 Before getting started,
34 
35 you may want to find out which IDEs and text editors are tailored to make Python editing easy,
36 
37 browse the list of introductory books,
38 
39 or look at code samples that you might find helpful.
40 
41 There is a list of tutorials suitable for experienced programmers on the BeginnersGuide/Tutorials page.
42 
43 There is also a list of resources in other languages which might be useful if English is not your first language.
  #每次读取完一个文件之后,都需要重新把这个文件再打开一次,之所以这样做是因为指针已经移到文件最后了。
44 >>> f.seek(0) #使用seek()移动指针,参数0为,指针回到文件最开始,
45 >>> f.readline()
46 'Before getting started, n'
47 >>> f.tell() #使用tell()查看当前指针的位置
48 26L
49 >>> f.seek(4) #参数为4,将指针定位到从开头到第4个字符的位置的后面
50 >>> f.tell()
51 4L
52 >>>

 

本文由67677新澳门手机版发布于计算机编程,转载请注明出处:Python文件和迭代

关键词: