[英]Python - How to detect a file of given type in a folder
I want to use Python to move .xlsx files if they are in a certain folder. 我想使用Python移动.xlsx文件(如果它们位于某个文件夹中)。 Is there a generic rule for this?
是否有通用规则?
What I am looking for is basically code for this: 我正在寻找的基本上是为此的代码:
if there is a file that is an excel document in /user/documents/folder: move it to trash. 如果/ user / documents / folder中有一个文件是excel文档:请将其移至回收站。
I'm happy with the shutil.move bit but what can I use to classify .xlsx files as a group? 我对shutil.move感到满意,但是如何将.xlsx文件分类为一组?
Thank you for your help everyone. 谢谢大家的帮助。 This turned out to be the easiest way of doing this
事实证明,这是最简单的方法
import os
folder = os.listdir(r'C:\Users\name\Desktop\somefolder')
for item in folder:
if item.endswith('.xlsx') == True:
print ('excel folder is in this folder')
I can then add a shutil.move function with ""if item.endswith('xlsx') == True"" to move the file elsewhere. 然后,我可以添加带有“” if item.endswith('xlsx')== True“”的shutil.move函数,以将文件移至其他位置。 Thanks for the support I.Renk and others :)
感谢您对I.Renk和其他人的支持:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.