Python---文件与文件夹操作

判断文件或文件夹是否存在

import os
dirs = r"E:\test"
file = r"E:\test\6.txt"

# 判断目录是否存在。若不存在则创建,可以创建多级目录
if not os.path.exists(dirs):
    os.makedirs(dirs)
# 判断文件是否存在。若不存在则创建,调用系统命令行来创建文件
if not os.path.exists(file):
    os.system(r"touch {}".format(file))
    os.system(r"touch %s" % file)

# 判断目录是否存在,如果不存在,则创建。此处是创建多级目录。
if not os.path.isdir(dirs):
    os.makedirs(file_dir)
# 判断文件是否存在,如果不存在,则创建。
if not os.path.isfile(file):
    os.system(r"touch %s" % file)


filename = "/home/mydir/test.txt"
file_dir = os.path.split(filename)[0]
print file_dir                     # 结果:/home/mydir
print filename                     # 结果:/home/mydir/test.txt
file = r"E:\test\1.txt"
dirs = os.path.split(file)[0]
print dirs                         # 结果:E:\test
print file                         # 结果:E:\test\1.txt

# file = "E:\\test\\1.txt"
# file = "E://test//1.txt"
# file = "E:/test/1.txt"
import pathlib

# pathlib模块在Python3版本中是内建模块,但是在Python2中是需要单独安装三方模块。
# 使用pathlib需要先使用文件路径来创建path对象。此路径可以是文件名或目录路径。
path = pathlib.Path("study_test")
res1 = path.exists()       # 检查路径是否存在
res2 = path.is_file()      # 检查路径是否是文件
print res1                 # 结果:True
print res2                 # 结果:False

# 参考:https://www.cnblogs.com/saneri/p/7495904.html
# 参考:https://www.runoob.com/w3cnote/python-check-whether-a-file-exists.html

判断文件是否可进行读写操作

import os

# os.F_OK: 检查文件是否存在;
# os.R_OK: 检查文件是否可读;
# os.W_OK: 检查文件是否可以写入;
# os.X_OK: 检查文件是否可以执行

if os.access("/file/path/foo.txt", os.F_OK):
    print "Given file path is exist."
if os.access("/file/path/foo.txt", os.R_OK):
    print "File is accessible to read"
if os.access("/file/path/foo.txt", os.W_OK):
    print "File is accessible to write"
if os.access("/file/path/foo.txt", os.X_OK):
    print "File is accessible to execute"

遍历文件夹下的所有文件和目录

# -*- coding:utf-8 -*-
import os

for root, dirs, files in os.walk(r"E:\test"):
    # 获取所有的文件
    for file in files:
        # 获取文件所属目录
        print(root)
        # 获取文件的名称
        print(file)
        # 获取文件的路径
        print(os.path.join(root, file))

    # 获取所有的目录
    for dir in dirs:
        # 获取目录的名称
        print(dir)
        # 获取目录的路径
        print(os.path.join(root, dir))

# 参考:https://blog.csdn.net/sinat_29957455/article/details/82778306
# -*- coding:utf-8 -*-
import os

def get_file_path(root_path, file_list, dir_list):
    # 获取该目录下所有的文件名称和目录名称
    dir_or_files = os.listdir(root_path)
    for dir_file in dir_or_files:
        # 获取目录或者文件的路径
        dir_file_path = os.path.join(root_path, dir_file)
        # 判断该路径为文件还是路径
        if os.path.isdir(dir_file_path):
            dir_list.append(dir_file_path)
            # 递归获取所有文件和目录的路径
            get_file_path(dir_file_path, file_list, dir_list)
        else:
            file_list.append(dir_file_path)

if __name__ == "__main__":
    # 根目录路径
    root_path = r"E:\test"
    # 用来存放所有的文件路径
    file_list = []
    # 用来存放所有的目录路径
    dir_list = []
    get_file_path(root_path, file_list, dir_list)
    print(file_list)
    print(dir_list)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值