Python读书笔记---文件与目录操作

(一)pathlib

pathlib是python3之后才有的特性,完全采用面向对象的编程方式,在处理路径方面非常方便。

 1.Path(from pathlib import Path)

主要方法:

Path.cwd()----返回一个代表当前路径的新的path object

Path.home()-----返回用户的home路径,也是一个新的path object

Path.stat()----返回路径的相关信息,使用方法如:p.stat().st_size    p.stat().st_mtime

Path.chmod()----改变文件的权限和方式

Path.exists()---path是否是存在的文件或者路径

Path.expanduser()---扩展路径

Path.glob(pattern)----找给定pattern的path,例:sorted(Path('.').glob('*.txt'))

Path.group()----返回拥有此文件的group

Path.is_dir()---判断path是否为目录

Path.is_file()---判断path是否为文件

Path.is_symlink()----判断path是否为一个符号链接

Path.is_socket()---判断path是否为一个unix socket

Path.is_fifo()---判断path是否指向一个FIFO

Path.iterdir()----当path是目录时,产生其目录内容

Path.lchmod(mode)---与chmod相似,仅当目标为链接时,改变的是链接的mode而不是target的mode

Path.lstat()-----与stat相似,当path为链接时,返回的是链接的信息而不是target的信息

Path.mkdir(mode=0o777, parents=False, exist_ok=False)----创建新目录

Path.open(mode='r', buffering=-1, encoding=None, newline=None)----打开path

Path.owner()----返回文件的拥有者

Path.read_bytes()---返回path的二进制内容

Path.read_text(encoding=None, errors=None)-----以字符串方式返回文件的解码内容

Path.rename(target)----重命名

Path.replace(target)-----重命名

Path.resolve(strict=False)-----得到绝对path

Path.rmdir()----删除目录,必须是空目录

Path.rglob(pattern)----与glob相似,在pattern前面加了**

Path.samefile(other_path)----判断是否为同一个文件

Path.symlink_to(target, target_is_directory=False)-----将path变为target的一个链接

Path.touch(mode=0o666, exist_ok=True)---创建文件

Path.unlink()----删除链接

Path.write_bytes(data)----以bytes模式打开文件,写入data,关闭文件

Path.write_text(data, encoding=None, errors=None)----以text模式打开文件,写入data,关闭文件。

 2.PurePath

略过

(二)os

1.os.path方法

os.path.abspath(path)----

os.path.basename(path)---

os.path.commonpath(paths)----

os.path.commonprefix(list)----

os.path.dirname(path)----返回path的目录名

os.path.exists(path)-----判断path是否存在

os.path.expanduser(path)----

os.path.expandvars(path)----

os.path.getatime(path)----返回最后进入path的时间

os.path.getmtime(path)----返回path最后一次被修改的时间

os.path.getctime(path)----

os.path.getsize(path)----返回path的大小,单位为bytes

os.path.isabs(path)----

os.path.isfile(path)-----

os.path.isdir(path)-----

os.path.islink(path)----

os.path.ismount(path)----

os.path.join(path, *paths)-----

os.path.normcase(path)-----

os.path.realpath(path)----

os.path.relpath(path, start=ps.curdir)---

os.path.samefile(path1, path2)---

os.path.sameopenfile(fp1, fp2)----

os.path.samestat(stat1, stat2)-----

os.path.split(path)----返回一个路径的目录名和文件名,已pair()的方式

os.path.splitdrive(path)----

os.path.splitext(path)----

os.path.splitunc(path)-----

 2.os中相关方法

os.chdir(path)---切换到path目录

os.getcwd()----返回当前工作目录

os.listdir()----返回指定目录下的所有文件和目录名

os.remove()----删除一个文件

os.removedirs()----删除多个目录

os.system()----运行shell命令

os.rename(old, new)---重命名

os.makedirs()----创建多级目录

os.makedir()---创建单个目录

os.stat(file)----获取文件属性

os.chmod(file)----修改文件权限与时间戳

os.mknod()----创建结点

 (三)shutil

shutil.copyfileobj(fsrc, fdst[,length])----

shutil.copyfile(src, dst, *, follow_syslinks=True)----

shutil.copymode(src, dst, *, follow_syslinks=True)----

shutil.copystat(src, dst, *, follow_syslinks=True)------

shutil.copy(src, dst, *, follow_symlinks=True)----

shutil.copy2(src, dst, *, follow_symlinks=True)------

shutil.ignore_patterns(*patterns)----

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)----

shutil.rmtree(path, ignore_errors=False, onerror=None)----

shutil.move(src, dst, copy_function=copy2)

shutil.disk_usage(path)---

shutil.chown(path, user=None, group=None)

shutil.which(cmd, mode=os.F_OK|os.X_OK, path=None)

 

实例一:创建多级目录

 1 import os
 2 import shutil
 3 print("the current path is: ",os.getcwd())
 4 os.chdir('C:\wgj\work\python')
 5 print('change path to: ',os.getcwd())
 6 a = 'test'
 7 path = '.'
 8 for i in range(1,5):
 9     a = '\\test' + str(i)
10     path = path + a
11 os.makedirs(path, 0o777)
12 print('after makedirs1111: ',os.getcwd())
13 #print(os.listdir('C:\wgj\work\python'))
14 if os.path.isfile('C:\wgj\work\python\hello.py'):
15     print('file exist')
16     os.chdir(path)
17     shutil.copy('C:\wgj\work\python\hello.py','.')
18 print(os.getcwd())

 

posted @ 2017-06-16 11:04  哈利波波特  阅读(8)  评论(0)    收藏  举报