文件操作

文件重命名:os.rename
删除文件:os.remove
创建文件夹:os.mkdir
获取当前目录:os.getcwd
删除文件夹:os.rmdir
获取当前文件:file
文件名截取:(p,name) = os.path.split()

# 文件操作
# 打开 关闭
# 读写
# 读:open('路径','模式:r,w,w+,a,a+',encoding='utf-8')
# 二进制文件:rb wb ab
# read readline readlines

# with open('路径','r',encoding='utf-8') as f:
#     f.read()
    #此方式不需要关闭文件

# 处理文件、文件夹。os 模块
import os
# os.mkdir('')
# os.rmdir('')
# 获取当前文件夹的绝对路径
os.getcwd()
# 获取当前文件的绝对路径
os.path.abspath(__file__)
print(__file__)
import shutil
#删除的文件夹下有文件
# shutil.rmtree('')

p = os.path.dirname(os.path.abspath(__file__))
print(os.getcwd())
print(p)
print("*"*10)
current_dir = os.getcwd()
test_file = os.path.join(current_dir,'data/test.txt')
print(test_file)
with open(test_file,'r',encoding='utf-8') as f:
    data = f.readlines()
    print(data,type(data))
    for i in range(0,len(data)):
        print(data[i])

# print(os.getcwd())
name = input()
file_name = os.path.join(os.getcwd(),'data/{}.txt'.format(name))
file_name1 = os.path.join(os.getcwd(),'data/{}副本.txt'.format(name))
with open(file_name,'r') as f:
    with open(file_name1,'w') as f1:
        f1.write(f.read())


# os.path.abspath(__file__)
print(__file__)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容