Python3 操作系统与路径 模块(os / os.path / pathlib)

来源:互联网 发布:网络货币有哪些 编辑:程序博客网 时间:2024/05/29 16:44

Python3 操作系统与路径 模块(os / os.path / pathlib)


本文由 Luzhuo 编写,转发请保留该信息.
原文: http://blog.csdn.NET/Rozol/article/details/71422712


以下代码以Python3.6.1 / windows10为例
Less is more!

#!/usr/bin/env python# coding=utf-8__author__ = 'Luzhuo'__date__ = '2017/5/7'import osdef os_demo():    # 执行命令    dirs = os.popen("dir").read()    print(dirs)    # 打印目录树    dirs_info = os.scandir()    for info in dirs_info:        print("文件名: {}, 路径: {}, inode: {}, 文件夹? {}, 文件? {}, 链接? {}".format(info.name, info.path, info.inode(), info.is_dir(), info.is_file(), info.is_symlink()))        stats = info.stat()        print(">>> 访问时间: {}, 修改时间: {}, 模式修改时间: {}, 大小: {}".format(stats.st_atime, stats.st_ctime, stats.st_mtime, stats.st_size))import signaldef os_func():    '''    操作系统模块        该模块下的方法,对各个版本的兼容不明确,须谨慎使用.        测试版本: Python:3.6.1   Windows:Windows10,64bit    '''    # === 系统 ===    strs = os.name  # 当前系统: Linux'posix' / Windows'nt' / 'ce' / 'java'    strs = os.sep  # 分隔符 \\ (windows:\\ linux:/)    strs = os.pathsep  # path变量分隔符 ; (windows:; linux::)    strs = os.linesep  # 换行分隔符 \r\n (windows:/r/n linux:\n)    dics = os.environ  # 查看系统环境变量(字典)    strs = os.getenv("Path", default=-1)  # 读取环境变量, 没有返回None    os.putenv("Path", "C:\\python")  # 添加环境变量  (windows无效)    os.unsetenv("Path")  # 删除环境变量  (windows不可用)    strs = os.getlogin()  # 当前登录的用户名    num = os.getpid()  # 当前进程PID    num = os.system("cmd")  # 执行操作系统命令, 返回0/1(0执行正确;1执行错误)    strs = os.popen("dir").read()  # 执行系统命令,并去读结果    tups = os.times()  # 当前进程时间(user用户 / system系统 / children_user子用户(windews=0) / children_system子系统(windews=0) / elapsed过去时间)    bytes = os.urandom(10)  # n字节用以加密的随机字符    num = os.cpu_count()  # CUP数量    # === 进程 ===    os.abort()  # 结束进程    # execl(file, *args) / execle / execlp / execlpe / execv / execve / execvp / execvpe // 运行新程序替换当前进程    os.execl(r"C:\python", 'python', 'hello.py', 'i')  # (windows执行失败)    os._exit(0)  # 退出    os.kill(8480, signal.SIGTERM) # (系统) 终止进程(需要导入:signal) SIGINT (终止进程) / SIGTERM (终止进程) / SIGKILL (终止进程) / SIGALRM (闹钟信号)    # === 文件 / 文件夹 ===    strs = os.getcwd()  # 当前路径    bytes = os.getcwdb()  # 当前路径    os.chdir(r"C:\Windows")  # 切换路径    strs = os.curdir  # 当前目录 .    strs = os.pardir  # 上级目录 ..    strs = os.sep  # 路径分隔符 ('/' or '\\')    bytes = os.fsencode(r"C:\c.obj")  # (编码) 文件路径字符串转为bytes类型 => b'C:\\c.obj'    strs = os.fsdecode(b"C:\c.obj")  # (解码) 文件路径转为strs类型 => 'C:\\c.obj'    # chmod(path, mode, *, dir_fd=None, follow_symlinks=True)    os.chmod(r"C:\python\hello.py", 777)  # 修改模式    os.link("file.txt", "file.txt.link")  # 创建硬链接    os.symlink("file.txt", "file.txt.link")  # 创建软链接 (windows执行失败)    lists = os.listdir()  # 所有文件和文件夹(列表) ".""..""D:"    # lstat(path, *, dir_fd=None)    tups = os.lstat(r"C:\c.obj")  # 状态信息列表    boolean = os.access(r"C:\c.obj", os.F_OK)   # (文件/文件夹) 权限测试 (mode: F_OK存在? / R_OK可读? / W_OK可写? / X_OK可执行?)    # scandir(path='.')  // DirEntry迭代器, 文件目录    lists = os.scandir()    # st_atime (访问时间) / st_ctime (修改时间) / st_mtime (模式修改时间) / st_size (大小(字节bytes))    # st_uid (用户ID) / st_gid (用户组ID)    # st_ino (inode) / st_mode (模式) / st_dev (设备) / st_nlink (硬链接)    # count = cpu_count() # (系统) CPU线程数(非核数)    tups = os.stat(".")  # 获取状态信息, 返回stat_result对象    # utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True) // 更新文件的访问和修改时间    num = os.utime(r"C:\c.obj")    # walk(top, topdown=True, onerror=None, followlinks=False) // 根目录(top)遍历目录树,返回迭代器 (dirpath, dirnames[], filenames[]).    root, dirnames, filenames = os.walk(r"C:\python")    os.removedirs(r"c:\python")  # 删除多个文件夹 (windews删除多个文件夹失败,单个成功)    # mkdir(path, mode=0o777, *, dir_fd=None) // 创建单个目录, 文件夹已存在,抛 FileExistsError 异常    os.mkdir("test")    # makedirs(name, mode=0o777, exist_ok=False)  // 创建(多个)文件夹    os.makedirs(r"./t1/t2/t3")    os.rmdir("test")  # 删除单个目录    # mknod(path, mode=0o600, device=0, *, dir_fd=None) // 创建空文件 (windows不可用)    os.mknod("test.txt")    # remove(path, *, dir_fd=None)    os.remove("test.txt")  # 删除文件    # rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)    os.rename("text.txt", "file.txt")  # 重命名    os.renames("text.txt", "file.txt")    # replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None) // 重命名, 存在则替换    os.replace("text.txt", "file.txt")    tups = os.stat(r"text.txt")  # 文件属性    # === 文件读写 ===    # open(path, flags, mode=0o777, *, dir_fd=None) // 打开文件 fd:文件描述符    fd = os.open(r"C:\c.obj", os.O_RDWR | os.O_CREAT)    readfd, writefd = os.pipe()  # 打开管道,返回读取,写入 (windows失败)    # fdopen(fd, *args, **kwargs) // 打开  (windews写入失败,读取""字符)    f = os.fdopen(readfd)    os.read(fd, 150)  # 读取    os.write(fd, "String".encode("utf-8"))  # 写入    os.fsync(fd)  # 强行写入    os.ftruncate(fd, 100)  # 裁剪文件    bytes = os.lseek(fd, 10, os.SEEK_SET)  # 设置指针  SEEK_SET(0开始) SEEK_CUR(1当前位置) SEEK_END(2末尾)    fd_temp = os.dup(fd)  # 副本    boolean = os.isatty(fd)  # 是否是tty设备    stat = os.fstat(fd)  # 状态信息    strs = os.device_encoding(fd)  # 返回终端字符编码,非终端None    os.close(fd)  # 关闭    os.closerange(fd, fd)  # 关闭并忽略错误    # === DirEntry ===    for dir in os.scandir():        strs = dir.name  # 文件名        strs = dir.path  # 完整路径名        num = dir.inode()  # inode编号        boolean = dir.is_dir()  # 是否是文件夹        boolean = dir.is_file()  # 是否是文件        boolean = dir.is_symlink()  # 是否是链接        tups = dir.stat()  # 状态信息的stat_result对象# ======================================================================def path_demo():    path = os.getcwd()  # 获取当前目录    print("路径: {}".format(path))    dirname = os.path.dirname(path)  # 获取文件夹名    print("文件夹名为: {}".format(dirname))    drive, path_t = os.path.splitdrive(path)  # 获取盘符    print("盘符为: {}".format(drive))def path_func():    '''    操作路径的函数    '''    paths = [r'file.txt', r"/python/lib/hello.py", r"/python/local/text.txt", "C:/python/local", "C:/python/file.txt"]    strs = os.path.abspath(paths[0])  # 绝对路径    strs = os.path.basename(paths[1])  # 文件名    strs = os.path.dirname(paths[1])  # 文件夹名    strs = os.path.join("C:\\", r"a.txt")  # 将路径组合返回    dirname, filename = os.path.split(paths[1])  # 分割路径 (目录 文件名)    strs, text = os.path.splitext(paths[1])  # 分离扩展名 (前部分, 扩展名)    drivename, pathname = os.path.splitdrive(paths[3])  # 分离目录 (盘符, 目录)    size = os.path.getsize(paths[0])  # 文件大小    strs = os.path.normcase(paths[1])  # 规范大小写 (修改斜杠)    strs = os.path.normpath(paths[1])  # 规范斜杠 (修改斜杠)    strs = os.path.realpath(paths[1])  # 规范名字 (全名)    # relpath(paths[0], start=os.curdir)    strs = os.path.relpath(paths[1])  # 当前路径的文件相对路径 => 'lib\\hello.py'    boolean = os.path.exists(paths[1])  # 路径是否存在    boolean = os.path.isabs(paths[1])  # 是否是绝对路径 (不准)    boolean = os.path.isfile(paths[1])  # 是否是文件    boolean = os.path.isdir(paths[1])  # 是否是文件夹    boolean = os.path.islink(paths[1])  # 是否是软链接    boolean = os.path.ismount("C:\\")  # 是否是根结点    boolean = os.path.supports_unicode_filenames  # Unicode是否可用作文件名    boolean = os.path.samefile(paths[0], paths[0])  # 是否指向同文件或目录    boolean = os.path.sameopenfile(os.open(paths[0], 1), os.open(paths[0], 1))  # fd是否指向同一文件    boolean = os.path.samestat(os.stat(paths[0]), os.stat(paths[0]))  # state是否指向同一文件    time_s = os.path.getatime(paths[0])  # 获取访问时间    time_s = os.path.getmtime(paths[0])  # 获取修改时间    time_s = os.path.getctime(paths[0])  # 元数据修改时间# ===================================================================================================import pathlibdef pathlib_demo():    path = pathlib.Path().home() / "python"    path_iter = path.iterdir()  # 遍历目录    for path in path_iter:        file_bool = path.is_file        if not file_bool:            continue        print(">>> {}".format(path))        f = path.open()  # 是文件就打开读点内容        data = f.readline()        print(data)        f.close()def pathlib_func():    '''    适合于不同操作系统的类来对文件系统路径进行操作    '''    # === PurePath (纯路径) ===    # PurePath(*pathsegments) // 系统地灵分割    path_pure = pathlib.PurePath('hello.py')    path_pure = pathlib.PurePath('python', 'hello.py')    path_pure = pathlib.PurePath(pathlib.Path('python'), pathlib.Path('hello.py'))    # PurePosixPath(*pathsegments) // 非windows文件系统路径    # PureWindowsPath(*pathsegments) // windows文件系统路径    # 路径是不可变哈希的, 同风格的路径是比较有序的    pathlib.PurePosixPath('hello') == pathlib.PurePosixPath('Hello')  # => Flase (PurePosixPath分大小写)    pathlib.PureWindowsPath('hello') == pathlib.PureWindowsPath('hello')  # => True (PureWindowsPath不分大小写)    pathlib.PureWindowsPath('hello') in {pathlib.PureWindowsPath('Hello')}  # => True    pathlib.PureWindowsPath('C:') < pathlib.PureWindowsPath('d:')  # => True    # 不同风格的路径不能比较有序    pathlib.PureWindowsPath('hello') == pathlib.PurePosixPath('hello')  # => False    # pathlib.PureWindowsPath('hello') < pathlib.PurePosixPath('hello')    # 运算符    path_pure = pathlib.PurePath('c:/')    path_pure = path_pure / 'python' / 'hello.py'  # 拼接路径    tups = path_pure.parts  # 返回组件元组 => ('c:\\', 'python', 'hello.py')    drive = path_pure.drive  # 盘符 => 'c:'    root = path_pure.root  # 根目录 => '\\'    driroot = path_pure.anchor  # 盘符 + 根目录 => 'c:\\'    parents = path_pure.parents  # 父路径列表 parents[0] => PureWindowsPath('c:/python')    parent = path_pure.parent   # 父路径 => PureWindowsPath('c:/python')    name = path_pure.name  # 文件(夹)名    suffix = path_pure.suffix  # 扩展名    suffixs = path_pure.suffixes  # 扩展名列表    name = path_pure.stem  # 不带后缀名的文件(夹)名 => 'hello'    path = path_pure.as_posix()  # 以/返回路径    path = path_pure.as_uri()  # 以file URL方式返回路径, 必须是绝对路径 => 'file:///c:/python/hello.py'    path = path_pure.joinpath("a.txt", "b.txt")  # 拼接路径 => PureWindowsPath('c:/python/hello.py/a.txt/b.txt')    path = path_pure.with_name("world.py")  # 更改路径文件(夹)名 => PureWindowsPath('c:/python/world.py')    path = path_pure.with_suffix(".txt")  # 更改扩展名,没有则添加    boolean = path_pure.match("*.py")  # 匹配通配符 (从右侧进行匹配) (大小写的区分见上方比较代码)    boolean = path_pure.is_absolute()  # 是否是绝对路径 (/ // c:/ 开头都被认为是绝对路径)    # === Path (具体路径) ===    # Path(*pathsegments)    path = pathlib.Path('hello.py')    # PosixPath(*pathsegments)    # WindowsPath(*pathsegments)    path_s = path.cwd()  # 当前路径    home = path.home()  # 用户主目录    state = path.stat()  # 该路径的状态信息 同os.stat()    state = path.lstat()  # 同stat(), 目标是软链接将返回软链接信息    path.chmod(777)  # 修改权限模式 同os.chmod()    path.lchmod(777)  # 同chmod(), 目标是软链接将更改软链接模式    lists = path.glob(r'*.py')  # 迭代器, 所有py文件  (windows失效)    group = path.group()  # 文件的组  (windows不可用)    iter = path.iterdir()  # 遍历目录 (必须是目录)    # mkdir(mode=0o777, parents=False, exist_ok=False)    path.mkdir()  # 创建文件夹    path.rmdir()  # 删除文件夹    # open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)  // 打开文件    f = path.open()    bytes = path.read_bytes()  # 读取文件内容    num = path.write_bytes(b"hello")  # (覆盖)写入文件内容    # read_text(encoding=None, errors=None)    strs = path.read_text(encoding='utf-8')    # write_text(data, encoding=None, errors=None)    num = path.write_text("hello")    ow = path.owner()  # 该文件拥有者 (windows不可用)    path.rename("new.txt")  # 重命名,目标存在则替换    path.replace("new.txt")  # 重命名,目标存在则替换    path_s = path.resolve()  # 绝对路径    path.symlink_to("hello.py", target_is_directory=False)  # 创建软链接, 如果目标是目录, 则(windows下)target_is_directory=True (windows失败)    path.unlink()  # 删除文件/文件软链接    # touch(mode=0o666, exist_ok=True)    path.touch()  # 创建文件    boolean = path.exists()  # 文件(夹)是否存在    boolean = path.is_dir()  # 是否是文件夹    boolean = path.is_file()  # 是否是文件    boolean = path.is_symlink()  # 是否是硬链接    boolean = path.is_block_device()  # 是否是块设备    boolean = path.is_char_device  # 是否是字符设备    boolean = path.samefile(path)  # path是否是同一文件if __name__ == "__main__":    os_demo()    path_demo()    pathlib_demo()    # os_func()    # path_func()    # pathlib_func()
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
原创粉丝点击