python 通过shutil实现文件后缀名判断及复制

In [18]: for file in os.listdir('.'):
    ...:     if os.path.splitext(file)[1] == '.html':
    ...:         print(file)
    ...:         r=shutil.copy(file,os.path.join('Desktop',file+'3'))
    ...:         print('copy path is '+r)
    ...:
index.html
copy path is Desktop\index.html3

 

获取扩展名和copy函数详解:

In [21]: help(os.path.splitext)
Help on function splitext in module ntpath:

splitext(p)
    Split the extension from a pathname.

    Extension is everything from the last dot to the end, ignoring
    leading dots.  Returns "(root, ext)"; ext may be empty.


In [22]: help(shutil.copy)
Help on function copy in module shutil:

copy(src, dst, *, follow_symlinks=True)
    Copy data and mode bits ("cp src dst"). Return the file's destination.

    The destination may be a directory.

    If follow_symlinks is false, symlinks won't be followed. This
    resembles GNU's "cp -P src dst".

    If source and destination are the same file, a SameFileError will be
    raised.

 

posted @ 2017-11-08 17:32  忙碌在路上  阅读(2081)  评论(0编辑  收藏  举报