<!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>&gt; Hi,
<br>&gt; 
<br>&gt; I'm trying to create a script to do the following. I have a directory
<br>&gt; containing hundreds of text files. I need to create a single file with
<br>&gt; the contents of all the files in the directory. Within that file,
<br>&gt; though, I need to create marks that indicate the division between the
<br>&gt; contents of each file that has wound up in that single file.
<br>&gt; 
<br>
<br>Your current code adds the marker line to the original files. &#32;Is that intended?
<br>
<br>You can open the output file for writing by passing 'w' as the second argument to open. &#32;You would do this before your directory-walking loop. &#32;Then when you read the contents of any file, just write to your output file as well, along with your marker line.
<br>
<br>&gt; I got this far but I'm stumped to continue:
<br>&gt; 
<br>&gt; ----------------- code--------
<br>&gt; import os
<br>&gt; path = '/Volumes/DATA/MyPath'
<br>&gt; os.chdir(path)
<br>&gt; file_names = glob.glob('*.txt')
<br>
<br>output_stream = open('outputfilename', 'w')
<br>
<br>&gt; for subdir, dirs, files in os.walk(path):
<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &#32;for file in files:
<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#32;f = open(file, 'r')
<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#32;text = f.readlines()
<br>
<br>output_stream.writelines(text)
<br>output_stream.write('______\n')
<br>
<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#32;f.close()
<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#32;f = open(file, 'a')
<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#32;f.write('\n\n' + '________________________________' + '\n')
<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#32;f.close()
<br>&gt; 
<br>
<br>output_stream.close()
<br>
<br>&gt; ------------
<br>&gt; 
<br>&gt; What's missing here is obvious. This iterates over all the files and
<br>&gt; creates the mark for the division at the end of each file. There is
<br>&gt; nothing, however, to pipe the output of this loop into a new file.
<br>&gt; I've checked the different manuals I own plus some more on the
<br>&gt; internet but I can't figure out how to do what's left.
<br>&gt; 
<br>&gt; I could get by with a little help from my Tutor friends.
<br>&gt; <br></p>
</body>
</html>