如何在python中的一个文件中从不同的文件夹同名制作多个文件

时间:2016-10-19 16:41:14

标签: python python-2.7 python-3.x

我想将来自不同文件夹数据的多个文件合并到一个文件中,但只有相同的文件名是所有文件夹

脚本:

import os

filenames = [os.path.join('C:/Users/Vishnu/Desktop/Test_folder/Input/','*.txt'), os.path.join('C:/Users/Vishnu/Desktop/Test_folder/Output/','*.txt')]
f = open(r'C:/Users/Vishnu/Desktop/Test_output/', 'wb')
for fname in filenames:
    with open(fname) as infile:
        for line in infile:
            f.write(line)

获取错误:

f = open(r"C:/Users/Vishnu/Desktop/Test_output/", "wb")
IOError: [Errno 13] Permission denied: 'C:/Users/Vishnu/Desktop/Test_output/'
>>> 

修改后

import os
import glob 
import os.mkdir

filenames = [glob.globos(mkdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' ,'Desktop','Test_folder','Input','*.txt'))), glob.glob(os.mkdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' , 'Desktop','Test_folder','Output','*.txt')))]    
filenames[0].extend(filenames[1])
filenames=filenames[0]

if( not os.path.isdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' , 'Desktop' ,'Test_folder', 'Test_output'))):
    os.mkdir(os.path.join(os.path.expanduser('~'),'C:', 'Users' , 'Vishnu' , 'Desktop' ,'Test_folder', 'Test_output'))
for fname in filenames:
    with open(fname) as file:
        for line in file.readlines():
            f = open(os.mkdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output')),'{:}.txt'.format(os.path.split(fname)[-1] ), 'a+')
            f.write(line)
            f.close()  

2 个答案:

答案 0 :(得分:1)

首先,您尝试打开文件夹本身。其次,我们必须在每次读取文件时关闭文件以避免权限问题

我试过这段代码。它现在应该工作

import os
import glob    #So that * in directory listing can be interpretted as all filenames

filenames = [glob.glob(os.path.join(os.path.expanduser('~'),'Desktop','Test_folder','Input','*.txt')), glob.glob(os.path.join(os.path.expanduser('~'),'Desktop','Test_folder','Output','*.txt'))]    
filenames[0].extend(filenames[1])
filenames=filenames[0]

if( not os.path.isdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output'))):
    os.mkdir(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output'))
for fname in filenames:
    with open(fname) as file:
        for line in file.readlines():
            f = open(os.path.join(os.path.expanduser('~'), 'Desktop', 'Test_output','{:}.txt'.format(os.path.split(fname)[-1] )), 'a+')
            f.write(line)
            f.close()    #This should take care of the permissions issue

答案 1 :(得分:0)

据我了解,您在多个目录中有多个文件。其中一些文件具有相同的名称,您希望将这些文件与一个文件的名称组合在另一个目录中。

第一项任务是弄清楚目录中的所有唯一文件名是什么;并收集这些独特文件的路径。

import os, glob
from collections import defaultdict

dirs = [r'/foo/bar/directory_1', r'/foo/bar/directory_2']
file_pattrn = r'*.txt'
unique_files = defaultdict(list)

for d in dirs:
  for i in glob.iglob(os.path.join(d, file_pattrn)):
     unique_files[os.path.basename(i)].append(i)

现在我们有了一个字典,其中键是文件名,值是该文件所有路径的列表。

一旦我们拥有了,那么我们只需循环遍历它们,即可创建新文件:

destination = r'/foo/bar/new_directory'
for unique_filename, copies in unique_files.items():
   with open(os.path.join(destination, unique_filename), 'w') as f:
     for copy in copies:
        with open(copy, 'r') as cp:
           for line in cp:
              f.write(line)