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()
相关推荐
Mh4 小时前
我是如何设计品牌站组件库的?
前端·css·设计模式
TomEval4 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
程序员大阳4 小时前
副队长Vue3教程(2)--Vite 搭建 Vue 项目,认识目录结构
前端·javascript·vue.js·vite·创建项目·目录结构·项目结构
濮水大叔5 小时前
Cabloy/Zova 与 Next.js 的 SSR 水合差异
前端·typescript·next.js
prince_zxill5 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
SoaringHeart5 小时前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
Resean02235 小时前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring
小毛球.6 小时前
第 3 篇:IDEA 最新版下载与完整安装教程(Mac)
java·macos·intellij-idea·idea
Thanks_ks6 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref
Flynt6 小时前
Chrome给AI编程助手塞了203篇前端规范,我拆包实测:检索0.77秒是真快,扫描器有点挑食
前端·chrome·ai编程