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()
相关推荐
2301_76415056几秒前
HTML图片怎么用UnoCSS对齐_UnoCSS原子化CSS图片对齐实战.txt
jvm·数据库·python
2401_883600252 分钟前
SQL处理分组聚合中的数据一致性_使用事务保证
jvm·数据库·python
xieliyu.4 分钟前
Java顺序表实现扑克牌Fisher-Yates 洗牌算法
java·数据结构·算法·javase
Cache技术分享5 分钟前
386. Java IO API - 监控目录变化
前端·后端
johnny2336 分钟前
Python Web服务器网关接口:WSGI、ASGI、RSGI、uWSGI、uwsgi、Gunicorn、Uvicorn
python
weixin_408717776 分钟前
为什么宝塔面板定时访问URL任务总是报502_检查目标接口响应时间与延长任务执行超时设置
jvm·数据库·python
YanDDDeat6 分钟前
【Spring】事务注解失效与传播机制
java·后端·spring
小陈工6 分钟前
python Web开发从入门到精通(二十七)微服务架构设计原则深度解析:告别拆分烦恼,掌握治理精髓(上)
后端·python·架构
源码之家8 分钟前
计算机毕业设计:Python渔业资源数据可视化分析大屏 Flask框架 数据分析 可视化 数据大屏 大数据 机器学习 深度学习(建议收藏)✅
人工智能·python·信息可视化·数据挖掘·数据分析·flask·课程设计
m0_746752309 分钟前
如何在导航栏中实现左右分列的菜单项布局
jvm·数据库·python