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()
相关推荐
wuhen_n4 小时前
LangChain 核心:Chain 链式调用实现复杂 AI 任务
前端·langchain·ai编程
往上跑山4 小时前
【Agentic RL / 强化学习 / OPD】OpenClaw-RL 源码阅读
前端
文心快码BaiduComate4 小时前
从个人效能到组织资产:文心快码企业版Agent Hub上线,提升团队AI编程效能
前端·后端·程序员
凯瑟琳.奥古斯特4 小时前
力扣1235:加权区间调度最优解
java·python·算法·leetcode·职场和发展
想不到ID了4 小时前
第八篇: 登录注册功能实现
java·javascript
咖啡星人k4 小时前
从需求到交付:我用MonkeyCode的AI Agent完成了一个React数据看板
前端·人工智能·react.js·monkeycode
sxlishaobin4 小时前
linux 自动清除日志 脚本
linux·服务器·前端
郑洁文4 小时前
基于Python的网络入侵检测系统
网络·python·php
ZC跨境爬虫5 小时前
跟着 MDN 学CSS day_37:(从文档流到粘性定位的底层原理)
前端·javascript·css·ui·html
AIMath~5 小时前
python中的uv命令揭秘
开发语言·python·uv