码迷,mamicode.com
首页 > 编程语言 > 详细

Python 学习笔记(十二)Python文件和迭代(一)

时间:2018-03-17 00:40:35      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:locking   which   文件的   option   __str__   指针   类型   元素   orm   

文件

文件和文件夹

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

文件夹:(windows) G:\pythonWorkspace\python\study   

    (linux/mac) /home/workspace/python

    注意:文件夹路径的斜杠linux与windows不同

windows下文件路径:示例 

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

 

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

查看属性:os.stat(filename)


p2 =r"G:\pythonWorkspace\python\study\test.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  whats 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 >>>

 

Python 学习笔记(十二)Python文件和迭代(一)

标签:locking   which   文件的   option   __str__   指针   类型   元素   orm   

原文地址:https://www.cnblogs.com/wangruihua-521/p/8563907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!