<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="generator" content="Osso Notes">
<title></title></head>
<body>
<p>
<br>----- Original message -----
<br>> Hi,
<br>>
<br>> I'm trying to create a script to do the following. I have a directory
<br>> containing hundreds of text files. I need to create a single file with
<br>> the contents of all the files in the directory. Within that file,
<br>> though, I need to create marks that indicate the division between the
<br>> contents of each file that has wound up in that single file.
<br>>
<br>
<br>Your current code adds the marker line to the original files.  Is that intended?
<br>
<br>You can open the output file for writing by passing 'w' as the second argument to open.  You would do this before your directory-walking loop.  Then when you read the contents of any file, just write to your output file as well, along with your marker line.
<br>
<br>> I got this far but I'm stumped to continue:
<br>>
<br>> ----------------- code--------
<br>> import os
<br>> path = '/Volumes/DATA/MyPath'
<br>> os.chdir(path)
<br>> file_names = glob.glob('*.txt')
<br>
<br>output_stream = open('outputfilename', 'w')
<br>
<br>> for subdir, dirs, files in os.walk(path):
<br>>  for file in files:
<br>>  f = open(file, 'r')
<br>>  text = f.readlines()
<br>
<br>output_stream.writelines(text)
<br>output_stream.write('______\n')
<br>
<br>>  f.close()
<br>>  f = open(file, 'a')
<br>>  f.write('\n\n' + '________________________________' + '\n')
<br>>  f.close()
<br>>
<br>
<br>output_stream.close()
<br>
<br>> ------------
<br>>
<br>> What's missing here is obvious. This iterates over all the files and
<br>> creates the mark for the division at the end of each file. There is
<br>> nothing, however, to pipe the output of this loop into a new file.
<br>> I've checked the different manuals I own plus some more on the
<br>> internet but I can't figure out how to do what's left.
<br>>
<br>> I could get by with a little help from my Tutor friends.
<br>> <br></p>
</body>
</html>