Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建文件夹时将文件移动到文件夹_Python_Directory - Fatal编程技术网

Python 创建文件夹时将文件移动到文件夹

Python 创建文件夹时将文件移动到文件夹,python,directory,Python,Directory,我有一些文件,我想把它们移到一些文件夹中。我根据文件数量编写了创建这些文件夹的代码。如何将文件移动到每个文件夹(最好是在创建每个文件夹时直接移动) 像这样的 os.rename()或shutil.move(),它们都有相同的语法 os.rename("path/to/current/file.foo", "path/to/new/desination/for/file.foo") shutil.move("path/to/current/file.foo", "path/to/new/desti

我有一些文件,我想把它们移到一些文件夹中。我根据文件数量编写了创建这些文件夹的代码。如何将文件移动到每个文件夹(最好是在创建每个文件夹时直接移动)

像这样的

os.rename()
shutil.move()
,它们都有相同的语法

os.rename("path/to/current/file.foo", "path/to/new/desination/for/file.foo")
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
就你而言

import os
import errno

src = (os.path.expanduser('~\\Desktop\\output8\\singola\\'))

causali = os.listdir(src)
causali.sort(key=lambda x: int(x.split('.')[0]))

for file in enumerate(causali):
   try:
        id_folder = os.makedirs(os.path.expanduser('~/test_move/{}'.format(file[0])))
        os.rename(os.path.expanduser('~\\Desktop\\output8\\singola\\{}'.format(file[1])),os.path.expanduser('~\\Desktop\\output8\\singola\\{}\\{}'.format(file[0],file[1])))
   except OSError as e:path/to/new/destination/for/file
        if e.errno != errno.EEXIST:
            raise
os.rename()
shutil.move()
,它们都有相同的语法

os.rename("path/to/current/file.foo", "path/to/new/desination/for/file.foo")
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
就你而言

import os
import errno

src = (os.path.expanduser('~\\Desktop\\output8\\singola\\'))

causali = os.listdir(src)
causali.sort(key=lambda x: int(x.split('.')[0]))

for file in enumerate(causali):
   try:
        id_folder = os.makedirs(os.path.expanduser('~/test_move/{}'.format(file[0])))
        os.rename(os.path.expanduser('~\\Desktop\\output8\\singola\\{}'.format(file[1])),os.path.expanduser('~\\Desktop\\output8\\singola\\{}\\{}'.format(file[0],file[1])))
   except OSError as e:path/to/new/destination/for/file
        if e.errno != errno.EEXIST:
            raise

下面的代码将为目录中的每个文件名创建子目录(文件夹),文件夹的名称将和文件名相同。并且每个文件都将被移动到具有相同名称的文件夹中

import glob, os, shutil                                                                     
source = "C:\\Users\\xx\\Desktop\\Folder"

for file_path in glob.glob(os.path.join(source, '*.*')):
    new_sub_folder = file_path.rsplit('.', 1)[0]
    os.mkdir(os.path.join(source, new_sub_folder))
    shutil.move(file_path, os.path.join(new_sub_folder, os.path.basename(file_path)))

下面的代码将为目录中的每个文件名创建子目录(文件夹),文件夹的名称将和文件名相同。并且每个文件都将被移动到具有相同名称的文件夹中

import glob, os, shutil                                                                     
source = "C:\\Users\\xx\\Desktop\\Folder"

for file_path in glob.glob(os.path.join(source, '*.*')):
    new_sub_folder = file_path.rsplit('.', 1)[0]
    os.mkdir(os.path.join(source, new_sub_folder))
    shutil.move(file_path, os.path.join(new_sub_folder, os.path.basename(file_path)))

你所有的文件都在一个目录下?你想把它们移到不同的目录吗?看看python库
shutil
@mtkilic我正在使用shutil,但我不理解执行此操作的字符串格式逻辑。我找到了另一种做这项工作的方法,但我不喜欢。你能更清楚地解释一下你想做什么吗。所以你们在目录中有一些文件,你们想把每个文件移到它自己的文件夹中吗?是的,没错。最好在创建文件夹后立即执行此操作。您的所有文件都在一个目录下?你想把它们移到不同的目录吗?看看python库
shutil
@mtkilic我正在使用shutil,但我不理解执行此操作的字符串格式逻辑。我找到了另一种做这项工作的方法,但我不喜欢。你能更清楚地解释一下你想做什么吗。所以你们在目录中有一些文件,你们想把每个文件移到它自己的文件夹中吗?是的,没错。最好是在创建文件夹后立即执行此操作。太好了,我现在确信它会更吸引你的注意力。干得好太好了,我现在确信它会更深入你的脑海。干得好