python 文件目录操作

复制代码
1.os 库
python 复制代码
print(os.getcwd())
print(os.getenv("JAVA_HOME"))
print(os.path.abspath("python数据库操作"))
print(os.path.relpath("e:\\code"))
print(os.listdir("e:\\code"))
print(os.path.isdir("E:\\code"))
print(os.path.exists("a.txt"))
#os.remove("E:\\code\\python-start-demo\\debug.log")
print(os.path.getsize("e:\\"))
  1. python 读写文件
python 复制代码
import shutil
import glob
import zipfile
import paperclip
#for file in glob.glob("e:\\code\\*"):
#    print(file)
# for dirName,sub_dirNames,fileNames in os.walk("e:\\code"):
#   print("当前工作目录:%s"%(dirName))
#   print("当前工作子目录:%s" %(sub_dirNames))
#   print("当前文件列表:%s" % (fileNames))
def readFile(srcPath):
    str_obj = ''
    with open(srcPath, 'r') as f:
        obj_list = f.readlines()
    for obj in obj_list:
        str_obj += obj.rstrip()
    print(str_obj)

def copyFile(srcPath,dstPath):
    with open(srcPath, 'rb') as f, open(dstPath,'wb') as f2:
      content = f.readlines()
      f2.writelines(content)

def zipFile(srcPath):
    zip = zipfile.ZipFile("algorithm.zip",'w')
    for fileName in glob.glob("E:\\code\\python-start-demo\\algorithm\\*"):
        zip.write(fileName)
    zip.close()

def readZip():
    zipFile = zipfile.ZipFile("algorithm.zip",'r')
    for name in zipFile.namelist():
        print(name)
    for info in zipFile.infolist():
        print(info.filename,info.compress_type,info.file_size,info.compress_size)

def unzip():
    zipFile = zipfile.ZipFile("algorithm.zip")
    zipFile.extractall("algorithm3")
    zipFile.close()

def paste():
    paperclip.copy("123")
    str = paperclip.paste()
    print(str)
#src_path = "E:\\code\\python-start-demo\\algorithm\\two_add.py";
#dest_Path = "E:\\code\\python-start-demo\\algorithm\\two_add_copy2.py";
#copyFile(src_path,dest_Path);
#shutil.copy(src_path,dest_Path)
#source_dir = "E:\\code\\python-start-demo\\algorithm"
#target_dir = "E:\\code\\python-start-demo\\algorithm2"
#shutil.copytree(source_dir,target_dir)
#shutil.move("E:\\code\\python-start-demo\\algorithm\\file_opt.py","E:\\code\\python-start-demo\\algorithm\\file_opt2.py")
zipFile("E:\\code\\python-start-demo\\algorithm")
readZip()
unzip()
paste()
相关推荐
风骏时光牛马7 分钟前
AI_Coding:智能代码生成与工程实践
前端
华研前沿标杆游学7 分钟前
企业参访入口 | 走进华为坂田基地,深圳华为坂田基地考察游学参访
python
颜进强13 分钟前
22 · NestJs InjectionScopes 注入作用域:默认单例不是偷懒,是最优——以及何时才该打破
前端·后端·ai编程
ShineWinsu17 分钟前
对于Redis:缓存的解析
java·redis·缓存·缓存穿透·缓存击穿·缓存雪崩·缓存预热
liangshanbo121521 分钟前
Web Worker 在 AI 前端应用中有什么具体使用场景?
前端·人工智能
rannn_11125 分钟前
【Java面试题】高频面试题1|Java 后端、MySQL、Redis 与 RAG 面试整理
java·jvm·数据库·后端·面试
写后端的胖头鱼28 分钟前
一文详解Spring 事务失效场景
java·spring·事务·transactional·事务传播机制
Q264336502333 分钟前
【有源码】基于Spring Boot和Vue的在线考试与交流系统-教育信息化背景下在线考试与交流一体化平台
java·vue.js·spring boot·mysql·spring·毕业设计·课程设计
拖孩38 分钟前
一个全程 AI 写的小程序「厨菜记」,上线 20 天跑通流量主,收入几块钱,开心得不行
前端·后端·微信小程序
颜进强40 分钟前
21 · NestJs AsyncProviders 异步提供者:useFactory 返回 Promise 之后,容器发生了什么
前端·后端·ai编程