porter:python中的简单文件操作
porter的Python项目详细描述
许可证
麻省理工学院。
安装
使用pip:
pip install porter
文件管理
导入端口:
import porter
创建文件夹:
porter.mkdir('/foo/bar') # The new directory 'bar' will be created.
重命名文件/目录:
porter.rename('/foo/bar.txt', 'file.txt') >>> '/foo/file.txt' porter.rename('/foo/bar', 'folder') >>> '/foo/folder'
删除文件/目录:
porter.remove('/foo/bar')
复制文件/目录:
# Two ways same result porter.copy('/foo/bar.txt', '/foo1/bar.txt') porter.copy_to('/foo/bar.txt', '/foo1')
移动文件/目录:
# Two ways same result porter.move('/foo/bar', '/foo1/bar') >>> '/foo1/bar' porter.move_to('/foo/bar', '/foo1') >>> '/foo1/bar'
忽略并强制:
porter.mkdir('/foo/bar', ignore=True) # If '/foo/bar' exists, porter will not create the folder and no error will occur. # Ignore option can ONLY ignore the error of the existing destination file/directory. porter.move('/foo/bar', '/foo1/bar', force=True) # If '/foo1/bar' exists, porter will move the directory anyway.
“mkdir”、“copy”、“copy”“to”、“move”、“move”“to”函数中都有ignore和force。
存档:
porter.archive('/foo/bar') >>> '/foo/bar.tar' porter.archive('/foo/bar', 'archive', 'zip') >>> '/foo/archive.zip' porter.archive_to('/foo/bar', '/foo/bar1', 'archive') >>> '/foo/bar1/archive.tar'
所有支持的存档类型:“gztar”、“bztar”、“tar”、“zip”
类TargetFile:
bar = porter.TargetFile('/foo/bar.txt') bar.src >>> '/foo/bar.txt' bar.name >>> 'bar' bar.ext >>> 'txt' bar.move_to('foo1') bar.src >>> '/foo1/bar.txt' bar.remove() # Then it will be removed.
类TargetDirectory:
""" The structure of the directory: - foo - bar - dir1 - f1.txt - dir2 - f2.txt - foo1 """ bar = porter.TargetDirectory('/foo/bar') bar.src >>> '/foo/bar' bar.list() >>> {'dir1': {'f1.txt': None}, 'dir2': {'f2.txt': None}} bar.files() >>> ['f1.txt', 'f2.txt'] bar.directories() >>> ['dir1', 'dir2'] bar.move_to('foo1') bar.src >>> '/foo1/bar' bar.empty() # All of the files in it will be removed bar.remove() # Then it will be removed.