简体   繁体   English

如何遍历给定目录内的所有文件并创建文件夹并移动文件?

[英]How do I iterate through all files inside a given directory and create folders and move the file?

I have a folder which contains around 500 files. 我有一个包含约500个文件的文件夹。

I have a problem where I am creating folders based on those 500 file names. 我在基于这500个文件名创建文件夹时遇到问题。 For eg, if I have files such as A.txt, B.txt, etc first I want to create folders named 'A' and 'B' and then push 'A.txt' file into a folder named 'A' and 'B.txt' named file into folder named 'B', which I have just created. 例如,如果我首先拥有A.txt,B.txt等文件,我想创建名为“ A”和“ B”的文件夹,然后将“ A.txt”文件推送到名为“ A”和“ B.txt”命名的文件到我刚刚创建的名为“ B”的文件夹中。 SO basically two tasks first is to create the folders based on the file names and then push the corresponding files into the named folders. 因此,基本上,两项任务首先是基于文件名创建文件夹,然后将相应的文件推送到命名文件夹中。

However, I'm getting stuck in two places, first, the folders name are getting created as 'A.txt', 'B.txt' etc instead on 'A' or 'B' as I am taking the filename itself and second is to place the files into corresponding folders. 但是,我被卡在两个地方,首先,文件夹名称被创建为“ A.txt”,“ B.txt”等,而不是在“ A”或“ B”上创建,因为我要使用文件名本身,其次是是将文件放入相应的文件夹。

I have tried below code: 我试过下面的代码:

       import os, shutil, glob
       import pandas as pd

       def i2f(directory):
       for filename in os.listdir(directory):
           foldername = filename
           folder_loc = "all_files\user\txt-images"
           crfolder(os.path.join(folder_loc, foldername))
           '''
           crfolder is function that creates a folder
           '''
           src_dir = r"all_files\user\txt-images\src_folder" 
           dstn_dir = r"all_files\user\txt-images\trgt_folder" 
           for file in glob.glob("\\*.txt"):
           re.compile(r"[^_.A-Z]")
           shutil.copy2(file, dstn_dir)

       def crfolder():
           import os
           try:
              if not os.path.exists(folder_loc):
                 os.makedirs(folder_loc)
           except OSError:
              print ('''Can't create directory! ''' +  folder_loc)

Any help is appreciated to tell me where I am getting it wrong. 如有任何帮助,请告诉我我在哪里弄错了。

to make things simple: 使事情变得简单:

import os, shutil
parent_folder = 'myfolder'

# get files only not folders
files = [name for name in os.listdir(parent_folder) if os.path.isfile(os.path.join(parent_folder, name))]

for f_name in files:
    file = os.path.join(parent_folder, f_name)  # full path

    folder_name = f_name.split('.')[0]  # remove file extension
    folder = os.path.join(parent_folder, folder_name)  # full path

    if not os.path.exists(folder):  # make folder if not existed before
        os.mkdir(folder)

    shutil.move(file, os.path.join(folder, f_name))  # move file

Try this: 尝试这个:

import os
import shutil

path = r"C:\Users\vasudeos\OneDrive\Desktop\newfolder"

for x in os.listdir(path):

    file = os.path.join(path, x)

    if not os.path.isdir(file):
        folder_name = os.path.join(path, os.path.splitext(x)[0])

        if not os.path.exists(folder_name):
            os.mkdir(folder_name)

        shutil.move(file, folder_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历文件夹以在Python中查找特定文件? - How do I iterate through folders to find particular files in Python? 如何将文件和文件夹移动到指定目录? - How do I move both files and folders to the specified directory? 遍历目录 h5 文件的所有文件夹 - Iterate/Loop through all folders of directory h5 files 如何修复Python函数以遍历目录中的JSON文件列表并合并为单个JSON文件 - How do I fix a Python Function to iterate through a list of JSON files in a directory and merge into a Single JSON file 如何遍历具有多个文件夹的目录中的文件,对文件进行操作,保存到不同的文件夹集 - how to iterate through files in directory with multiple folders, operate on files, save to different set of folders 如何使用文件名创建文件夹,然后将文件移动到文件夹中? - How to create folders using file names and then move files into folders? 如何在目录中移动文件而不是文件夹? - How do you move files, but not folders, within a directory? 如何遍历目录以查找文件夹 - How to iterate through directory to find folders 如何在所需文档之前遍历文件夹? - How do I iterate through folders before the wanted document? 如何遍历给定目录中的文件? - How can I iterate over files in a given directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM