Author:AXYZdong 自动化专业 工科男
文件的两个关键属性:文件名 和 路径(表示文件在计算机上的位置)
Windows 上,路径的书写使用倒斜杠 \
作为文件夹之间的分隔符
OS X 和 Linux 上,使用正斜杠 /
作为路径的分隔符。
当前工作目录
每个运行在计算机上的程序,都有一个“当前工作目录”,或cwd。没有从根文件夹开始的文件名或路径,都假定在当前工作目录下。os.getcwd() 函数获取当前工作路径的字符串,可以利用 os.chdir() 改变它。
>>>os.getcwd() # 获取当前工作路径,cwd(current work directory):当前工作路径
'D:\\Python Study'
绝对路径与相对路径
用 os.makedirs() 创建新文件夹
>>>import os
>>>os.makedirs('.\\read1')
>>>os.makedirs('D:\\Python study\\read2')
os.makedirs('.\\read1')
:相对路径,表示在当前工作目录下创建read1文件夹。
os.makedirs('D:\\Python study\\read2')
:绝对路径,在D盘的Python Study文件夹下创建read2文件夹。
为了确保完整路径名存在,如果中间文件夹不存在,os.makedirs()将创建所有必要的中间文件夹。
os.path 模块
os.path 模块包含了许多与文件名和文件路径相关的有用函数。os.path 是 os 模块中的模块,import os 就可以导入它。os.path 模块的完整文档:http://docs.python.org/3/library/os.path.html 。
处理绝对(absolute)路径和相对(relative)路径
os.path.abspath(path)
返回参数的绝对路径的字符串,便于相对路径转换为绝对路径。os.path.isabs(path)
判断参数是否是一个绝对路径,如果是则返回 True,否则返回 False。os.path.relpath(path,start)
将返回从 start 到 path 的相对路径的字符串,若没有提供 start ,就默认当前工作目录作为开始路径。os.path.dirname(path)
将返回一个字符串,包含 path 参数中最后一个斜杠之前的所有内容。(即返回目录名称)os.path.basename(path)
将返回一个字符串,包含 path 参数中最后一个斜杠之后的所有内容。(即返回基本名称)os.path.split(path)
同时返回一个路径的目录名称和基本名称,获得包含这两个字符串的元组。>>>import os
>>>path = 'C:\\Windows\\System32\\calc.exe'
>>>os.path.abspath(path)
'C:\\Windows\\System32\\calc.exe'
>>>os.path.isabs(path)
True
>>>os.path.relpath(path,'C:\\Windows')
'System32\\calc.exe'
>>>os.path.dirname(path)
'C:\\Windows\\System32'
>>>os.path.basename(path)
'calc.exe'
>>>os.path.split(path)
('C:\\Windows\\System32', 'calc.exe')
查看文件大小和文件夹内容
os.path.getsize(path)
返回 path 路径下的文件夹大小。os.listdir(path)
返回 path 路径下文件名字符串的列表。>>>path = os.getcwd()
>>>os.path.getsize(path)
4096
>>>os.listdir(path)
['.idea', 'Data Visualization', 'demo', 'demo.py', 'demo_1.py', 'paper.png', 'PDF合成', 'text.py', 'turtle', 'watermark', 'wordcloud', '__pycache__', '灯笼.py', '爬图片.py', '面纱.jpg']
检查路径的有效性
>>>path = os.getcwd()
>>>os.path.exists(path)
True
>>>os.path.exists('D:\\read')
False
>>>os.path.exists('D:\\Python study')
True
>>>os.path.isfile(path)
False
>>>os.path.isdir(path)
True
>>>os.path.isfile('D:\\Python Study\\demo.py')
True
利用 os.path.exists() 函数 可以确定DVD或者闪盘当前是否连在计算机上。 (我当前电脑上是没有F盘的)
>>>os.path.exists('F:\\')
False
纯文本文件
:只包含基本文本字符,不包含字体、大小和颜色信息。如:带有 .txt 扩展名的文本文件,带有 .py 扩展名的Python脚本文件。
读写文件的三个步骤:
open 打开文件的几种模式
Character Meaning --------- ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
在当前目录下创建 one.txt 文本文件,并先后写入 Hello world!和 my name is axyzdong。
>>>onefile = open('one.txt','w') #以写模式打开文本
>>>onefile.write('Hello world!\n')
13
>>>onefile.close()
>>>onefile = open('one.txt','a') #以添加模式打开文本
>>>onefile.write('my name is axyzdong')
19
>>>onefile.close()
>>>onefile = open('one.txt')
>>>content = onefile.read()
>>>onefile.close()
>>>print(content)
Hello world!
my name is axyzdong
使用 shelve 模块将 Python 程序中的变量保存到二进制的 shelf 文件中。
步骤:
shelve.open() 打开的文件默认既能读又能写,和字典一样 shelf 值有 keys() 和 values() 方法,返回 shelf 中的键和键值。
使用场合:假设有一个字典,保存在一个变量中,你希望保存这个变量和它的内容,这时就可以使用 pprint.pformat() 函数来保存这个变量,并将它写入 .py 文件。这个文件将变成一个模块,可以在需要的时候调用它。
>>>import pprint
>>>characters = {'one':'a','two':'b'}
>>>pprint.pformat(characters)
"{'one': 'a', 'two': 'b'}"
>>>file = open('characters.py','w')
>>>file.write('characters = '+ pprint.pformat(characters) + '\n')
38
>>>file.close()
>>>import characters
>>>characters.characters
{'one': 'a', 'two': 'b'}
>>>characters.characters['one']
'a'
这样,当前工作目录下就会有一个 characters.py 文件,里面只包含 一行代码 characters = {'one': 'a', 'two': 'b'}
创建一个 .py
文件的好处在于,.py
文件是一个文本文件,任何人都可以使用简单的文本编辑器读取和修改该文件的内容。
参考文献
1:Python编程快速上手:让繁琐工作自动化/ (美)斯维加特(A1 Sweigart) 著;王海鹏译.北京:人民邮电出版社,2016.7
本次的分享就到这里
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。