天下男修都躺好,江户盗贼团五叶ost,魔力家族第二季
在计算机中,文件是以二进制的形式保存在磁盘上的;
文本文件
二进制文件
在计算机中操作文件的步骤时分固定,一共包含三个步骤:
在python中操作文件需要记住一个函数和两个方法
序号 | 函数/方法 | 说明 |
---|---|---|
1 | open | 打开文件,返回文件操作对象; |
2 | read | 将文件内容读取到内存; |
3 | write | 将指定内容写入到文件; |
4 | close | 关闭文件; |
open函数的第一个参数是要打开的文件名(文件名区分大小写);
如果文件存在,返回文件操作对象 如果文件不存在,会抛出异常
read方法可以一次性读入并返回文件的所有内容;
close方法负责关闭文件;
如果忘记关闭文件,会造成系统资源消耗,而且会影响到后续对文件的访问;
注意:read方法执行后,会把文件指针移动到文件的末尾;
提示:在开发中,通常会先编写打开和关闭的代码,再编写中间针对文件的读写操作;
读取文件示例
# 打开文件 file = open("hello.txt") # 读取文件 text = file.read() print(text) # 关闭文件 file.close() # welcome to hello.txt # this is a test file.
所以,如果在同一次打开文件中,执行了一次read方法,读取所有内容,再次调用read方法的话,就不能够再获得内容了;这是因为第一次读取之后,文件指针移动到了文件末尾,再次调用不会读取到任何的内容;
文件指针示例
# 打开文件 file = open("hello.txt") # 读取文件 text = file.read() print(text) print(len(text)) print("-"*50) # 再次读取文件 text1 = file.read() print(text1) print(len(text1)) # 关闭文件 file.close() # welcome to hello.txt # this is a test file. # 42 # -------------------------------------------------- # 0
open函数默认以只读方式打开文件,并且返回文件对象
open函数语法如下:
f = open("文件名", "访问方式")
open打开文件情况罗列
访问方式 | 说明 |
---|---|
r | 以只读方式打开文件。文件的指针将会放在文件的开头,这是默认模式。如果文件不存在,抛出异常 |
w | 以只写方式打开文件。如果文件存在会被覆盖。如果文件不存在,创建新文件 |
a | 以追加方式打开文件。如果文件已存在,文件指针将会放在文件的结尾。如果文件不存在,创建新文件进行写入。 |
r+ | 以读写方式打开文件。文件的指针将会放在文件的开头。如果文件不存在,抛出异常。 |
w+ | 以读写方式打开文件。如果文件村子会被覆盖。如果文件不存在,创建新文件。 |
a+ | 以读写方式打开文件。如果该文件已存在,文件指针将会放在文件的结尾。如果文件不存在,创建新文件进行写入。 |
打开方式其实还有"rb","wb","wb+",...等,这是针对二进制文件的方式。
提示
追写文件示例
file = open("hello.txt", "a") file.write("hello world") file.close()
readline方法
读取指定行数数据
file = open("hello.txt") line1 = file.readline() print(line1) line2 = file.readline() print(line2) file.close() # 1.welcome to hello.txt # 2.this is a test file.
分行读取整个文件:
hello.txt内容
1.welcome to hello.txt 2.this is a test file. 3.hello world
readline_test.py内容
file = open("hello.txt") while 1: line = file.readline() print(line, end="") if not line: break file.close() # 运行结果 # 1.welcome to hello.txt # 2.this is a test file. # 3.hello world
# 打开文件 read_file = open("hello.txt") write_file = open("hello_small.txt", "w") # 读写文件 text = read_file.read() write_file.write(text) # 关闭文件 read_file.close() write_file.close()
# 打开文件 read_file = open("hello.txt") write_file = open("hello_big.txt", "w") # 读写文件 while 1: # 读取一行内容 text = read_file.readline() # 判断是否有内容 if not text: break # 写入复制文件 write_file.write(text) # 关闭文件 read_file.close() write_file.close()
序号 | 方法名 | 说明 | 示例 |
---|---|---|---|
01 | rename | 重命名文件 | os.rename(源文件名,目标文件名) |
02 | remove | 删除文件 | os.remove(文件名) |
序号 | 方法名 | 说明 | 示例 |
---|---|---|---|
01 | listdir | 目录列表 | os.listdir(目录名) |
02 | mkdir | 创建目录 | os.mkdir(目录名) |
03 | rmdir | 删除目录 | os.rmdir(目录名) |
04 | getcwd | 获取当前目录 | os.getcwd() |
05 | chdir | 修改工作目录 | os.chdir(目标目录) |
06 | path.isdir | 判断是否是文件 | os.path.isdir(文件路径) |
提示:文件/目录操作都支持绝对路径/相对路径
在python2.x文件的第一行增加以下代码,解释器会以utf-8编码来处理python文件;
# *-* coding:utf8 *-*
上面这种是官方推荐使用的,也可以使用下面这种方式:
# coding=utf8
例如下面的代码:
test_str = "hello世界" print(test_str)
在2和3下都会正确执行,但是当我们遍历输出字符串时,就会出现不一样的情况;
test_str = "hello世界" print(test_str) for i in test_str: print(i)
在python3中可以正常执行以上代码;
但是在python2中,可以输出完整字符,遍历时输出的结果中"世界"显示的是六行特殊字符;
解决为 定义字符串时,需要在字符串的引号前,增加一个小写字母u。
示例如下:
test_str = u"hello世界" print(test_str) for i in test_str: print(i)
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论