Hi,
I have just started using Python and I am slowly getting into it. I wanted to make a little script to merge all files in a directory into one.
All of these files will be text files.
I know I need to use os.path.walk() to walk through all the files, and if not os.path.isdir(filename) with open(filename,'r').read() to read all the files that are not directories.
Can someone give me a clue or a very simple example that I can work with please?
Thanks in advance.
6 30538 bvdet 2,851
Expert Mod 2GB
Hi,
I have just started using Python and I am slowly getting into it. I wanted to make a little script to merge all files in a directory into one.
All of these files will be text files.
I know I need to use os.path.walk() to walk through all the files, and if not os.path.isdir(filename) with open(filename,'r').read() to read all the files that are not directories.
Can someone give me a clue or a very simple example that I can work with please?
Thanks in advance.
I think this code will do what you want: - import os
-
-
def dir_list(dir_name, subdir, *args):
-
'''Return a list of file names in directory 'dir_name'
-
If 'subdir' is True, recursively access subdirectories under 'dir_name'.
-
Additional arguments, if any, are file extensions to add to the list.
-
Example usage: fileList = dir_list(r'H:\TEMP', False, 'txt', 'py', 'dat', 'log', 'jpg')
-
'''
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
if len(args) == 0:
-
fileList.append(dirfile)
-
else:
-
if os.path.splitext(dirfile)[1][1:] in args:
-
fileList.append(dirfile)
-
-
# recursively access file names in subdirectories
-
elif os.path.isdir(dirfile) and subdir:
-
# print "Accessing directory:", dirfile
-
fileList += dir_list(dirfile, subdir, *args)
-
return fileList
-
-
def combine_files(fileList, fn):
-
f = open(fn, 'w')
-
for file in fileList:
-
print 'Writing file %s' % file
-
f.write(open(file).read())
-
f.close()
-
-
if __name__ == '__main__':
-
search_dir = "C:/directory"
-
fn = "output_file.txt"
-
combine_files(dir_list(search_dir, False, 'txt'), fn)
Thanks , you have helped a lot :)
another way -
import os,shutil
-
f=open("/tmp/fileappend.txt","a")
-
for r,d,fi in os.walk("/home/me"):
-
for files in fi:
-
if files.endswith(".txt"):
-
g=open(os.path.join(r,files))
-
shutil.copyfileobj(g,f)
-
g.close()
-
f.close()
-
I think this code will do what you want: - import os
-
-
def dir_list(dir_name, subdir, *args):
-
'''Return a list of file names in directory 'dir_name'
-
If 'subdir' is True, recursively access subdirectories under 'dir_name'.
-
Additional arguments, if any, are file extensions to add to the list.
-
Example usage: fileList = dir_list(r'H:\TEMP', False, 'txt', 'py', 'dat', 'log', 'jpg')
-
'''
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile):
-
if len(args) == 0:
-
fileList.append(dirfile)
-
else:
-
if os.path.splitext(dirfile)[1][1:] in args:
-
fileList.append(dirfile)
-
-
# recursively access file names in subdirectories
-
elif os.path.isdir(dirfile) and subdir:
-
# print "Accessing directory:", dirfile
-
fileList += dir_list(dirfile, subdir, *args)
-
return fileList
-
-
def combine_files(fileList, fn):
-
f = open(fn, 'w')
-
for file in fileList:
-
print 'Writing file %s' % file
-
f.write(open(file).read())
-
f.close()
-
-
if __name__ == '__main__':
-
search_dir = "C:/directory"
-
fn = "output_file.txt"
-
combine_files(dir_list(search_dir, False, 'txt'), fn)
This seemed to work for a while for me now it comes up with "NameError: name 'False' is not defined"
bvdet 2,851
Expert Mod 2GB
This seemed to work for a while for me now it comes up with "NameError: name 'False' is not defined"
- >>> bool(0)
-
False
-
>>> bool(1)
-
True
-
>>>
'0' and '1' can be substituted for 'False' and 'True' respectively. The question is what happened to 'False' on your system?
- >>> bool(0)
-
False
-
>>> bool(1)
-
True
-
>>>
'0' and '1' can be substituted for 'False' and 'True' respectively. The question is what happened to 'False' on your system?
Did a quick reset on my machine and is working now. very strange.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Guillaume Lahitette |
last post by:
Hello everyone,
I am looking for a lightweight Java Applet to diff and merge two text
files. The functionality would be a subset of what WinMerge
(http://winmerge.sourceforge.net) offers:
-...
|
by: Charlie |
last post by:
Hello,
I have data in an Access table that I would like to export to multiple
HTML tables. I would like to split the data in the Access table
(about 92,000 records) into multiple HTML...
|
by: Chris Murphy via DotNetMonster.com |
last post by:
Hey all, just wondering if anyone can point me in the right direction. I'm
developing a solution that allows a user to store multiple text-based
content (like code snippets, notes, documents etc.)...
|
by: Buddhist[CHinA] |
last post by:
The text files are not only the .txt files, but also all ascii files.
Thx.
|
by: malla |
last post by:
Hi,
I am trying to open a bunch of files and get data from them into one
single file. I am sure this process can be automated if I name the
files that I want to open in a regular pattern (say...
|
by: ManningFan |
last post by:
I'm running through a database in a loop, and each time the loop
finishes I would like to export the values of some variables to new
text files. In reality it's more complex than this, but this is...
|
by: emaghero |
last post by:
Hello all, I want to open multiple txt files with similar names in C++
I have attempted this with the following code
//Create as many txt files as there are valid propagation constants...
|
by: as001 |
last post by:
Hi,
I'm writing a windows application in C# using VS 2003.
I got stuck where it has to write multiple output text files. Here's my piece of code:
for loop
{
Random r = new Random();...
|
by: maral |
last post by:
Hi every one, this is my first post here!
I'm using GATE toolkit for information retrieval and text analysis, but i really need java for some parts.
I have managed to find a specific word in...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
| |