2

I know this isn't the most concise way of writing this function, but I'm still learning! My goal is to combine all the text files in a given directory into one main text file.

The first time I run this function, everything works perfectly. The new, merged file is saved into the same directory as the original files. When i try to re-run the function, I get an error message!

def merge_files(path):
    ''' mergers txt files into one  main file'''
    now = datetime.datetime.now()
    date = ("%s_%s_%s_") %(now.month,now.day,now.year)
    files_to_merge = os.listdir(path)
    merged_file_list = []
    counter = 1
    for file in files_to_merge:
        if file.endswith(".txt"):
            with open(path+("file"+str(counter))+".txt","r") as files:
                files = files.readlines()
                merged_file_list.append(files)
                counter = counter + 1
    new_list = ("\n").join(["".join(x) for x in merged_file_list])
    print (new_list)
    with open (path + date + "final.txt" ,"w") as final:
        return final.write(new_list)

Here's the error message I get:

IOError: [Errno 2] No such file or directory: '/Users/star/Desktop/udemy/Sample-Files/file4.txt'

I know there's something wrong with the counter but I'm not sure what!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
StarskyNY
  • 35
  • 1
  • 3
  • I suspecyt that the problem is that your merged files are taken into account the second time since they're in the same dir and with txt extension. But maybe it's intentional? At any rate, the behaviour is different the second time because directory contents is different. – Jean-François Fabre Oct 21 '16 at 19:49
  • The error message is telling you that your file does not exists at that location. – Tammo Heeren Oct 21 '16 at 19:50
  • The error message tell you that the file not found, I think the way you generated the file location, you can simply use `open(os.path.join(path, file), "r") ` – Yassine Sedrani Oct 21 '16 at 20:05

1 Answers1

1

Don't use path+("file"+str(counter))+".txt", use os.path.join(path, file) (or more simply in Mac: path + '/' + file). It's not clear why you want to generate the filenames when you have them right there.

What's happening is that at first you have file1.txt, file2.txt, and file3.txt. You run the script and the filenames you calculate with the counter work correctly, so it completes fine. It creates a new file <date>final.txt so now the directory has 4 files. Then you run the script again and the loop runs 4 times because now there's 4 files and so it tries to find file4.txt but there's no reason it should be there.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89