os、sys及shutil

os模块

os

os.getcwd()/ os.getcwdb()    

返回当前目录/返回当前目录的字节码 b‘dir’;注:linux:pwd

os.chdir(path)

改变当前工作目录   注:win/linux:cd  path

os.curdir   --> ‘.’

os.pardir   -->‘..’

os.makedirs(name)/ os.removedirs(name)

创建 / 删除 单个或多个递归目录(也删除空目录) 注:linux:mkdir -p name;rm  -fr  name

os.mkdir(name)/ os.rmdir(name)

创建单级目录 / 删除单级目录  注:linux:mkdir  name;rm  -fr   name

os.listdir(path)

列出指定路径中内容如当前os.listdir(‘.’) 注:linux:类似ls

os.rename(old,new)

重命名

os.stat(name)

返回文件/目录信息

os.sep

返回当前平台的分隔符  win:'\'     linux:'/'

os.linesep

返回当前平台的换行符          win:' '  linux:' '

os.pathsep

返回当前平台环境变量(通过os.environ查看)中不同路径的分隔符  win:分号   linux:冒号

os.name

返回当前平台类型   win:'nt'   linux:'posix'

os.system(command)

执行bash命令如os.system(‘tree’)

os.popen(cmd,mode='r',buffering=-1)

os.path

os.path.abspath(path)

return  an  absolute path  注:不判断path是否存在

os.path.basename(p)

Returns the final component of a pathname  注:不判断path是否存在

os.path.dirname(p)
Returns the directory component of a pathname  注:不判断path是否存在

os.path.split(p)

Split a pathname. Returns tuple "(head, tail)" where "tail" is   everything after the final slash. Either part may be empty    例:os.path.split('hh')     # ('', 'hh')   注:不判断path是否存在

os.path.exists(path)
Test whether a path exists.

os.path.isabs(path)

判断是否是绝对路径    注:不判断path是否存在

os.path.isdir(p)

p是否存在且为一个目录

os.path.isfile(p)

p是否存在且为一个文件

os.path.join(path, *paths)

Join two (or more) paths. 

# win
>>> os.path.join('myfile1','myfile2','file.txt')
'myfile1\myfile2\file.txt'
>>> os.path.join('myfile1' 'myfile2','file.txt') 'myfile1myfile2\file.txt'
>>> os.path.join('c:','myfile1','myfile2','file.txt') 'c:myfile1\myfile2\file.txt'
>>> os.path.join('c:\','myfile1','myfile2','file.txt') 'c:\myfile1\myfile2\file.txt' # linux In [266]: os.path.join('myfile1','myfile2','file.txt') Out[266]: 'myfile1/myfile2/file.txt' In [267]: os.path.join('myfile1' 'myfile2','file.txt') Out[267]: 'myfile1myfile2/file.txt' In [268]: os.path.join('','myfile1','myfile2','file.txt') Out[268]: 'myfile1/myfile2/file.txt' In [269]: os.path.join('/','myfile1','myfile2','file.txt') Out[269]: '/myfile1/myfile2/file.txt' In [270]: os.path.join('/','/myfile1','myfile2','/file.txt') Out[270]: '/file.txt' In [271]: os.path.join('/','myfile1','myfile2','/file.txt') Out[271]: '/file.txt' In [272]: os.path.join('myfile1','myfile2','/file.txt') Out[272]: '/file.txt'

sys模块

sys.version

解释器版本信息

sys.path

模块搜索路径的字符串列表

sys.platform

返回平台名称 win32或linux

shutil模块

shutil.copyfileobj(fsrc, fdst, length=16384)
copy data from file-like object fsrc to file-like object fdst

def copyfileobj(fsrc, fdst, length=16*1024):
    """copy data from file-like object fsrc to file-like object fdst"""
    while 1:
        buf = fsrc.read(length)
        if not buf:
            break
        fdst.write(buf)
View Code
In [24]: shutil.copyfileobj(open('111.txt'),open('123.txt','w'))

In [25]: ls

shutil.copyfile(src,dst)

In [27]: shutil.copyfile('111.txt','1234.txt')    #自己不用打开文件即可复制,文件不存在自动创建
Out[27]: '1234.txt'

shutil.copymode(src,dst)

只复制权限,其他不复制

shutil.copystat(src,dst)

只复制权限,最后访问时间,最后修改时间

shutil.copy(src,dst)

Copy data and mode bits ("cp src dst"). Return the file's destination.

shutil.copy2(src,dst)

在copy基础上+最后访问及修改时间

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=<function copy2 at 0x7f337e150f28>, ignore_dangling_symlinks=False)
Recursively copy a directory tree.

shutil.rmtree(path, ignore_errors=False, onerror=None)
Recursively delete a directory tree.

shutil.move(src, dst, copy_function=<function copy2 at 0x7fdd27b25f28>)

Recursively move a file or directory to another location. This is   similar to the Unix "mv" command. Return the file or directory's  destination.

shutil.make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)

Create an archive file (eg. zip or tar).

'base_name' is the name of the file to create, minus any format-specific
extension; 'format' is the archive format: one of "zip", "tar", "bztar"
or "gztar".

'root_dir' is a directory that will be the root directory of the
archive; ie. we typically chdir into 'root_dir' before creating the
archive.  'base_dir' is the directory where we start archiving from;
ie. 'base_dir' will be the common prefix of all files and
directories in the archive.  'root_dir' and 'base_dir' both default
to the current directory.  Returns the name of the archive file.

'owner' and 'group' are used when creating a tar archive. By default,
uses the current owner and group.
View Code
渐变 --> 突变
原文地址:https://www.cnblogs.com/lybpy/p/7806076.html