项目中用到python判断一个字符串是否以某个字符串结尾,比如,筛选一个目录下所有以.mp4结尾的文件。

>>> item = "demo.mp4"
>>> item.endswith('.mp4')
True
>>> item.endswith('.json')
False
>>> item.startswith('demo')
True
>>> item.startswith('index')
False
>>>

例子:

将/tmp目录下所有的mp4文件转移到/home目录下

import shutil
import os
file_list = os.listdir('/tmp')
for item in file_list:
    if item.endswith('.mp4'):
        shutil.move('/tmp/%s' %item, '/home/%s' %item)

 

相关文章:

  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
猜你喜欢
  • 2022-01-01
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案