简体   繁体   English

Python-如何在文件夹中检测给定类型的文件

[英]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.

相关问题 如何使用python检测文件夹中的文件? - How to detect file within folder with python? 给定一个hdfs路径,我怎么知道它是一个文件夹还是一个带有python的文件 - Given a hdfs path, how do I know if it is a folder or a file with python 如何在python文件中列出给定类型的所有类? - How do I list all classes of a given type in a python file? Python 测试给定文件夹路径中文件的给定路径 - Python test given path to file in given path of folder 如何确定给定对象在Python中是否具有给定类型? - How to determine if the given object is of given type in Python? 如何从子文件夹运行 python 文件,以便当前文件夹文件路径自动检测和编辑数据? - How to run python file from subfolder so current folder file path automatically detect and edit data? Python:检测文本文件中是否只出现一个给定的单词 - Python: detect if just one of the given words appear in text file 如何正确应用 RE 以从给定路径获取姓氏(文件或文件夹的)并将其打印在 Python 上? - How to correctly apply a RE for obtaining the last name (of a file or folder) from a given path and print it on Python? 自动检测文件夹中的文件 - auto detect file in a folder Python:获取给定文件夹中特定文件类型的名称 - Python: get the names of specific file types in a given folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM