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.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM