python读写模块

一.创建目录,文档等

1.查看当前路径

import os     #导入os模块
cur_loc= os.getcwd()      #调用os模块的 getcwd()方法。返回值为程序工作的当前目录
print(‘当前目录为:’, cur_loc)       #打印目录

2.创建目录

path=os.getcwd()+'/'+'hello'
if not os.path.exists(path) :     #没有这个文件目录则新建一个
    os.mkdir(path )     #创建G盘文件名为 hello 的文件夹

3.改变工作目录

os.chdir(path)
os.getcwd()

4.在改变后的目录下创建一个txt的文件

f = open('a.txt', 'w')
f.write('hello,')
f.write('iplaypython')
f.close()

在改变后的目录下创建k个txt文件,并写入

for j in range(k):
    with open('diff_match%d.txt'% j,'w+') as temp:
        temp.write('hello,')
        temp.close()

如果只想创建备用,不写入,只是open即可。重新open的时候,旧的写入结果会被覆盖
参考链接

二 . 读写模块

# 读取总txt文件
# 按行读取
open_diff = open('diff_match_image.txt', 'r')
diff_line = open_diff.readlines()
 
line_list = []
for line in diff_line:
    line_list.append(line)
# print(line_list)
 
#切分diff
diff_match_split = [line_list[i:i+100] for i in range(0,len(line_list),100)]
 
#将切分的写入多个txt中
for i,j in zip(range(0,13),range(0,13)):
    with open('diff_match%d.txt'% j,'w+') as temp:
        for line in diff_match_split [i]:
            temp.write(line)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值