用Python读写文件 — OS模块相关知识点

一、相关概念
1、文件&文件路径
对‘c:\windows\pw.py’来说:‘c:\windows’是路径,‘pw.py’是文件名,‘py’是文件的拓展名
注:windows、OS X系统上文件名不区分大小写,linux上区分

2、路径
(1).路径格式:
windows : ‘c:\\windows\\pw.py
OS X/Linux : ‘c/windows/pw.py’
(2).绝对路径&相对路径
绝对路径:总是从根文件夹开始,如 c:\windows\pw.py
相对路径:相对于程序当前的工作目录来说的一个路径,如:…\pp\a.txt。其中‘.\’表示当前文件夹(可省略),‘…\’表示父文件夹

3、一些常用函数
os.path.join(path1,path2,path3) : 在当前系统下,返回包含正确路径分隔符的字符串

os.path.join(‘c:\\’,‘windows’,‘python36’) → ‘c:\\windows\\python36’
os.path.join(’…’,‘windows’,‘python36’) → ‘…\\windows\\python36’
根文件夹需用‘c:\\’、‘d:\\’

os.getcwd() : 获取当前工作目录

os.getcwd() → ‘c:\\python36’

os.chdir(path) : 更改工作目录到path下

os.makedir(path) : 创建新文件夹,将创建所有必要的中间文件夹

os.path.exists(path) : 判断文件/文件夹是否存在
os.path.isdir(path) : 判断path是否是文件夹
os.path.isfile(path) : 判断path是否是文件
os.path.isabs(path) : 判断path是否是绝对路径
os.path.dirname(path_file) : 返回路径最后一个斜杠前的所有内容(path)
os.path.basename(path_file) : 返回路径最后一个斜杠后的所有内容(file)
os.path.split(path_file) : 返回以上两个字符串元组(‘c:\windows’,‘pw.py’)

os.path.abspath(path) : 返回path的绝对路径

os.path.abspath(’.’) → ‘c:\\python36’

os.path.relpath(path,start) : 返回start到path的相对路径

os.path.relpath(‘c:\\windows’,‘c:\\spam\eggs’) → ‘…\\…\\windows’

os.getsize(path) : 返回文件的字节数

os.listdir(path) : 返回路径文件夹中文件名字字符串列表

os.listdir('c:\\python36\\写的程序‘) → [‘pw.txt’ , ‘ab.doc’ , ‘一些练习.pyw’]

二、文件读写过程(适用于纯文本文件)
1、步骤
(1).open(path),打开一个文件,返回一个File对象
(2).调用File对象的read()或write()方法
(3).调用File对象的close()方法,关闭该文件

2、相关函数、方法
(1).open(path_file , r|w|a)函数

open(path_file,‘r’) → 等价于open(path_file)
open(path_file,‘w’) #覆盖文本
open(path_file,‘a’) #文末添加文本

当path存在,file不存在时:
r会报错:[Errno 2] No such file or directory: ‘c:\python36\capitalquiz1.txt’
w\a会创建一个文件

当path不存在时:
r\w\a都会报错

(2).read()方法
File.read() : 返回文件内容,即一个长字符串
File.readlines() : 返回一个字符串列表,每个字符串是文本的一行,包含’\n’
示例:

import os

helloFile = open(‘c:\\windows\\hello.txt’)

helloContent = helloFile.read()
print (helloContent)
→ ‘hello world!
how are you’
print ([helloContent]) → [‘hello world!\nhow are you!’]

helloContent2 = helloFile.readlines()
print ([helloContent2]) → [‘hello world!\n’ , ‘how are you!’]

(3).write(str)方法
用open(path,w|a)模式打开文件时,才能用.write(str)
将返回写入的字符个数,包括换行符

(3).close()方法
读取或写入(w|a)文件后,要调用.close()方法后,才能再次打开该文件

三、用shelve模块、pprint.pformat()函数保存变量
待补充

################实践中用到的一些不认识的函数################
1、random.shuffle(list)

list = [a,b,c,d]
random.shuffle(list) //每调用一次,list随机排序一次
print (list) → [b,c,d,a]
random.shuffle(list)
print (list) → [c,b,d,a]

random.sample(seq, n)
从序列seq中选择n个随机且独立的元素

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回
print (slice) → [1, 4, 10, 8, 5]

2、dic的keys()、values()的数据Type是:

capitals.keys() dict_keys([‘Beijing’, ‘Shanghai’, ‘Hubei’, ‘Hunan’, ‘Anhui’, ‘Hainan’, ‘Guangxi’, ‘Shanxi’, ‘Gansu’, ‘Guangdong’, ‘Jiangxi’, ‘Zhejiang’])
type(capitals.keys()) <class ‘dict_keys’>

要转换成list数据Type:

list(capitals.keys()) → [‘Beijing’, ‘Shanghai’, ‘Hubei’, ‘Hunan’, ‘Anhui’, ‘Hainan’, ‘Guangxi’, ‘Shanxi’, ‘Gansu’, ‘Guangdong’, ‘Jiangxi’, ‘Zhejiang’]

3、list.index[str] & list.remove(str) & del list[n]//列表有讲,给忘记了

list = [‘a’,‘b’,‘c’,‘d’]
list.index(‘a’) → 0

list.remove(‘a’)
print (list) → [‘b’,‘c’,‘d’]

del list[0] //一次只能删除1个
print (list) → [‘b’,‘c’,‘d’]

4、’%s’%(i+1)
怎么对字符串赋值的,学习ing,待补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值