Colderleo's blog Colderleo's blog
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)

Colder Leo

热爱代码的打工人
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)
  • 1-django使用
  • centos部署Nginx+uwsgi+django - blog
  • django-admin完全自定义某个模块的页面
  • easy_select2 编辑页面添加外键选择搜索
  • 使用vscode开发python
  • python自定义异常处理with,上下文context管理器
  • python读写excel,xlsx 模块选择
  • python多线程多进程协程
  • TemporaryFile
  • threading用法
  • ubantu设置python优先级
  • conda anacodna 命令行用法
  • 我写的常用工具函数
  • 0-python常用函数
  • smtp发送邮件
  • pandas用法
  • datetime类,时间差
  • format
  • enumerate遍历数组时获取index
  • argv
  • generator 生成器
  • GIL锁、多线程
  • linux用源文件安装python3
  • list sort排序
  • logging日志
  • mulitporcessing共享变量
  • OrderedDict
  • path
  • pip用法
  • pymysql用法 - python连接mysql
  • python bash解释器,脚本前两行,
  • python docstring格式 PEP 257
  • python logging获取logger信息
  • python交互式窗口如何进行多行输入
  • virtualenv用法
  • 标准差
  • 单例模式
  • 函数中定义static变量
  • 切片
  • 去掉字符串中emoji字符
  • 去掉字符串中的空行
  • 全局变量、global和nonlocal
  • 文字识别pytesseract
  • 析构函数和del
  • 用python制作游戏外挂
  • 正则表达式,函数替换字符串
  • 装饰器
  • pycharm中运行pyqt时不报错
  • python 写文件
  • Python
gaoliu
2021-10-06

path

# 创建文件夹

folder = os.path.join('aaa', 'bbb')
os.makedirs(folder, exist_ok=True)
1
2

# 删除

os.remove(file) #只能删除文件,不能删除文件夹

shutil.rmtree(folder_name) #删除文件夹
1
2
3

# 复制

shutil.copyfile(arg.out_file, arg.in_file)
1

# split分割路径

fullpath = " D:/download/repository.7z ".strip()

#  case 1 分隔路径和文件名:
folder_path, filename = os.path.split(fullpath)
print (folder_path)  # D:/download
print (filename)  # repository.7z

#  case 2, 分隔扩展名:
folder_path, ext = os.path.splitext(fullpath)
print (folder_path)  # D:/download/repository
print (ext)  # .7z 注意这里有个点

#  case 3, 分隔盘符:
drv, left = os.path.splitdrive(fullpath);
print (drv) # D:
print (left) # /download/repository.7z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

查看文件或文件夹是否存在

print os.path.exists("/untitled/chapter3.py")

print os.path.exists("chapter3.py")
1
2
3

# 遍历文件夹

https://www.cnblogs.com/wt7018/p/11610286.html

import os


# 遍历文件夹
def walkFile(file):
    for root, dirs, files in os.walk(file):

        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list, 包括子目录中的文件

        # 遍历文件
        for f in files:
            filename = os.path.join(root, f)
            print(filename)

        # 遍历所有的文件夹
        for d in dirs:
            folder = os.path.join(root, d)
            print(folder)

def main():
    walkFile("f:/ostest/")


if __name__ == '__main__':
    main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

遍历某个文件夹下的所有文件和目录,不包括子目录下的文件

for file_name in os.listdir(tar_dir):
    print(file_name)

1
2
3

寻找某个文件


def search_file(path,name):

    for root, dirs, files in os.walk(path):  # path 为根目录
        if name in dirs or name in files:
            return os.path.join(root, name)

1
2
3
4
5
6
7

# 判断是文件还是文件夹

if os.path.isdir(path):
    print "it's a directory"
elif os.path.isfile(path):
    print "it's a normal file"
else:
    print "it's a special file(socket,FIFO,device file)"
1
2
3
4
5
6
编辑 (opens new window)
上次更新: 2022/05/18, 01:07:09
OrderedDict
pip用法

← OrderedDict pip用法→

最近更新
01
通过模板实现结构体成员拷贝-按成员名匹配
05-07
02
c++17通过模板获取类成员的个数
05-01
03
avx-sse切换惩罚
04-30
更多文章>
Theme by Vdoing | Copyright © 2019-2023 Colder Leo | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×