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()
相关推荐
thefool1122663 小时前
Java 方法重载
java
索西引擎3 小时前
【React】JSX:JavaScript 语法扩展的本质、编译机制与表达能力分析
前端·javascript·react.js
这不小天嘛9 小时前
JAVA八股——J集合篇
java·开发语言
hboot9 小时前
AI工程师第五课 - 大语言模型基础
python·llm·fastapi
AOwhisky10 小时前
Python 学习笔记(第一期与第二期)——基础语法——核心知识点自测与详解
开发语言·笔记·python·学习·云原生·运维开发
STLearner11 小时前
ICML 2026 | LLM×Graph论文总结[1]【图基础模型,文本属性图,多模态属性图,图对齐,图提示学习,关系深度学习
论文阅读·人工智能·python·深度学习·学习·机器学习·数据挖掘
国科安芯11 小时前
航天电子模拟前端三大支柱:精密运放、高速运放与电压监控的协同设计方法——ASL8522S/ASL622S/ASL706S技术解析
前端·单片机·嵌入式硬件·fpga开发·架构·安全性测试
神明不懂浪漫11 小时前
【第四章】CSS(二)——文本外观属性与复合选择器
前端·css·经验分享·笔记
kyriewen12 小时前
别再用AI debug了——这5种bug越修越烂
前端·javascript·ai编程
习明然12 小时前
我的本地化AI项目(三)
人工智能·python·electron·c#·avalonia