【Python之旅】第二篇(一):Python文件处理

简介:

说明:

    主要是file()和open()函数的使用,但在查open()函数的帮助时,会有下面的说明:

1
2
3
>>> help(open)
……
Open a file using the file() type, returns a file object.

    因此,两个函数其实都是一样的,下面只用file()。

    在列举file()的作用时,使用help即是很好的方法,下面则是应重点关注的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
close(...)
  |      close() -> None or (perhaps) an integer.  Close the file.
flush(...)
  |      flush() -> None.  Flush the  internal  I/O buffer.
readline(...)
  |      readline([size]) -> next line from the file,  as  a string. 
readlines(...)
  |      readlines([size]) -> list of strings,  each  a line from the file.
seek(...)
  |      seek(offset[, whence]) -> None.  Move to  new  file position.
tell(...)
  |      tell() -> current file position, an integer (may be a long integer).
write(...)
  |      write(str) -> None.  Write string str to file.
writelines(...)
  |      writelines(sequence_of_strings) -> None.  Write the strings to the file.
xreadlines(...)
  |      xreadlines() -> returns self.




1.创建文件


--基本格式:

1
2
3
f = file( 'test.txt' 'w' )
f = write( 'Hello World!' )
f.close()

·w:写模式,文件不存在就创建,存在则自动覆盖原来的内容,只能写,不能读;

·w+:写读模式,但一开始还是会清空原来文件内容,只是在写文件之后可以读取;

·写的内容放在内存当中,如果要写入磁盘,可以f.close()关闭文件或f.flush()实时写入磁盘;

·不可以实时改变模式,只能把文件关闭后,再次打开时定义模式;


--实例:

1
2
3
4
5
>>> f = file( 'test.txt' 'w' )
>>> f.write( 'Hello World!' )
>>> f.flush()
xpleaf@xpleaf-machine:~/seminar6/day2$ more test.txt
Hello World!


--write()与writelines()

·前者写入的内容只能是字符串,后者则可以写入列表:

1
2
3
4
5
6
>>> f.write([ 'a' 'b' 'c' ])
Traceback (most recent call last):
   File  "<stdin>" , line  1 in  <module>
TypeError: expected a character buffer object
>>> f.writelines([ 'a' 'b' 'c' ])
>>>


--f.close()的重要说明

·如果没有f.close(),则在程序运行结束后,系统会自动帮我们关闭文件;

·长时间运行的程序,需要打开并编辑文件(如用'a'模式),没有关闭文件,会导致文件内容无法保持一致性的问题(如果系统中有其他程序需要编辑该文件);

·Linux中的Vim编辑器自带文件锁定功能,即不能同时编辑同一文件;

·Python中文件的锁是没有加上的,需要开发者自行为文件加锁。




2.读取文件与遍历文件内容


--基本格式:

1
2
3
f = file( 'test.txt' 'r' ) ===>可以不加 'r' ,默认就是该模式
f = read()
f.close()

·r:默认;

·r+:读写模式,可以尝试使用,每读取一行,指针就跳到下一行,写的时候,就直接覆盖掉指针指的这一行;

·rb:在windows平台下编辑的文件,在linux中用python进行读取时,模式要选择“rb”,否则有可能会出现乱码的现象,即跨平台的文件都要注意此点;


--read()、readline、readlines()与xreadlines()

·前三者都是直接把文件内容全部写入内存当中,然后再全部读取或一行一行地读取;

·都采用迭代的方式读取,即指针最开始指向第一行,读取第一行后,指针指向下一行;


-read()

·把文件内容全部读取:

1
2
3
4
5
>>> f = file( 'test.txt' 'r' )
>>> f.read()
"Hello World!\nI'm xpleaf.\nNice to meet you!\n"
>>> f.read()
''             ===>内容已经读完,即指针已经在最后一行,后面没有内容

·可以用tell()查看当前指针的位置:

1
2
>>> f.tell()
43             ===> 43 ,即是最后一个字符

·重新读取文件内容,可以f.close()后再次打开,也可以使用f.seek(0):

1
2
3
4
5
6
7
>>> f.seek( 0 )    ===>重新寻址,让指针指向文件最开始
>>> f.tell()
0
>>> print f.read()
Hello World!
I'm xpleaf.
Nice to meet you!


-readline()

·以字符串方式,一行一行地读取文件内容:

1
2
3
4
5
6
7
8
9
>>> f.seek( 0 )
>>> f.readline()
'Hello World!\n'
>>> f.readline()
"I'm xpleaf.\n"
>>> f.readline()
'Nice to meet you!\n'
>>> f.readline()
''


-readlines()

·以列表的方式,一行一行地读取文件内容,一行即为列表中的一个元素:

1
2
3
4
5
>>> f.seek( 0 )
>>> f.readlines()
[ 'Hello World!\n' "I'm xpleaf.\n" , 'Nice to meet you!\n']
>>> f.readlines()
[]

·因此,习惯性的用法是:修改文件内容

1
2
3
4
5
6
7
>>> f.seek( 0 )
>>> filelist = f.readlines()
>>> print filelist
[ 'Hello World!\n' "I'm xpleaf.\n" , 'Nice to meet you!\n']
>>> filelist[ 2 ] =  'See you next time!'
>>> print filelist
[ 'Hello World!\n' "I'm xpleaf.\n" , 'See you next time!']

·再以w的方式打开文件,用f.writelines(filelist)的方式写入,即可实现修改文件内容的目的;


-xreadlines()

·不是先把文件内容全部写入内存,而是每读取一行才写入一行,写下一行时即对前面内存中的内容进行回收;

·在读取较大文件时,适宜采用这种办法。


--文件内容的遍历:使用readlines()

1
2
3
4
5
6
7
8
>>> f = file( 'test.txt' 'r' )
>>> filelist = f.readlines()
>>>  for  eachline  in  filelist:
...   print eachline,
... 
Hello World!
I'm xpleaf.
Nice to meet you!




3.文件内容追加


--基本格式:

1
2
3
f = file( 'test.txt' 'a' )
f = write( 'Hello World!' )
f.close()

·文件内容追加到最后一行上,如果最后一行有'\n',则追加到下一行;

·write只能添加字符串,如果是数值或其它类型的数据类型,则需要使用str()进行转换;


--实例:

1
2
3
4
5
6
7
8
9
>>> f = file( 'test.txt' 'a' )
>>> f.write( 'See you next time!' )
>>> f.write( 'I will miss you much!\n' )
>>> f.flush()
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
Hello World!
I'm xpleaf.
Nice to meet you!
See you next time!I will miss you much!




4.文件内容替换


--基本格式:

1
2
3
4
import  fileinput
for  line  in  fileinput.input( 'filepath' , inplace =  1 ):
   line = line.replace( 'oldtext' 'newtext' )
   print line,

·inplace = 1,表示要修改文件内的内容,默认值为0,表示不修改文件内容,加“print line,”时只打印内存中修改的内容(看下面例子);

·inplace = 1时,如果不加“print line,”,原来文件内容会为空;

·可以额外加backup参数,表示在修改文件内容时进行备份;


--实例:


-正确操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
>>>  import  fileinput
>>>  for  line  in  fileinput.input( 'test.txt' , inplace =  1 , backup =  '.ori' ):
...   line = line.replace( 'Hello World!' 'Hello, everyone!' )
...   print line,
... 
xpleaf@xpleaf-machine:~/seminar6/day2$ ls -l test*
-rw-rw-r--  1  xpleaf xpleaf  87   9 月   4  15 : 32  test.txt
-rw-rw-r--  1  xpleaf xpleaf  83   9 月   4  15 : 19  test.txt.ori
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
Hello, everyone!
I'm xpleaf.
Nice to meet you!
See you next time!I will miss you much!


-如果没有加inplace = 1时:

1
2
3
4
5
6
7
8
9
10
11
12
13
>>>  for  line  in  fileinput.input( 'test.txt' ):
...   line = line.replace( 'Nice' 'Well' )
...   print line,
... 
Hello, everyone!
I'm xpleaf.
Well to meet you!
See you next time!I will miss you much!
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
Hello, everyone!
I'm xpleaf.
Nice to meet you!
See you next time!I will miss you much!


-如果没有加“print line,”时:

1
2
3
4
5
6
7
8
>>>  for  line  in  fileinput.input( 'test.txt' ):
...   line = line.replace( 'Nice' 'Well' )
... 
>>>  for  line  in  fileinput.input( 'test.txt' , inplace =  1 ):
...   line = line.replace( 'Hello' 'Hey' )
... 
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
xpleaf@xpleaf-machine:~/seminar6/day2$     ===>文件内容已被清空
相关文章
|
3天前
|
Python
Python代码扫描目录下的文件并获取路径
【5月更文挑战第12天】Python代码扫描目录下的文件并获取路径
20 1
|
3天前
|
存储 JSON 数据库
Python中列表数据的保存与读取:以txt文件为例
Python中列表数据的保存与读取:以txt文件为例
17 2
|
3天前
|
存储 NoSQL MongoDB
MongoDB数据库转换为表格文件的Python实现
MongoDB数据库转换为表格文件的Python实现
35 0
|
3天前
|
Shell Python
Python Stock guess_indicators_daily_job.py文件的调整
Python Stock guess_indicators_daily_job.py文件的调整
16 1
|
3天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
【5月更文挑战第10天】BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
22 1
|
1天前
|
Python
Python办公自动化|自动整理文件,一键完成!
Python办公自动化|自动整理文件,一键完成!
|
3天前
|
Python
Python知识点——文件和数据格式化
Python知识点——文件和数据格式化
6 0
|
3天前
|
存储 JSON 安全
Python中的文件操作与文件IO操作
【5月更文挑战第14天】在Python中,文件操作是常见任务,包括读取、写入和处理文件内容。`open()`函数是核心,接受文件路径和模式(如&#39;r&#39;、&#39;w&#39;、&#39;a&#39;、&#39;b&#39;和&#39;+&#39;)参数。本文详细讨论了文件操作基础,如读写模式,以及文件IO操作,如读取、写入和移动指针。异常处理是关键,使用`try-except`捕获`FileNotFoundError`和`PermissionError`等异常。进阶技巧涉及`with`语句、`readline()`、`os`和`shutil`模块。数据序列化与反序列化方面,介绍了
17 0
|
3天前
|
Python
python如何读取excel文件,并修改内容?
python如何读取excel文件,并修改内容?
8 0
|
3天前
|
Unix Linux 数据处理
使用Python批量复制文件夹及其子文件夹下的指定文件
使用Python批量复制文件夹及其子文件夹下的指定文件
14 1