1
import os

exts = ['ppt', 'pptx', 'doc', 'docx', 'txt', 'pdf', 'epub']
files = []

for root, dirnames, filenames in os.walk('.'):
    for i in exts:
        for file in filenames:
            if file.endswith(i):
                file1 = os.path.join(root, file)
                print(file1)
                with open(os.getcwd()+ r"\ally_"+i+".txt", 'w+') as f:
                    f.write("%s\n" % file1)

I m trying this code. How do I write all files in my system with ex. doc extention into a file named all_docs.txt in my desktop? file.write() inside for loop only write the last line of each extention into the files.

ilanos
  • 193
  • 6

3 Answers3

0

You need to open the log file in append mode (a) and not in write mode (w), because with w the file gets truncated (all content deleted) before anything new is written to it.

You can look into the docs of open(). This answer also has an overview of all the file modes.

Does it work with a for you?

Ralf
  • 16,086
  • 4
  • 44
  • 68
0
with open(os.getcwd()+ r"\ally_"+i+".txt", 'w+') as f:
    f.write("%s\n" % file1)

According to https://docs.python.org/2/library/functions.html#open the "w+" operation truncates the file.

Modes 'r+', 'w+' and 'a+' open the file for updating (reading and writing); note that 'w+' truncates the file.

RonJohn
  • 349
  • 8
  • 20
0

The mode w+ for open causes to truncate the file, this is the reason for losing the lines, and only the last one will stay there. An other little problem can be that this method of joining the path and the file name is not portable. You should user os.path.join for that purpose.

            with open(os.path.join(os.getcwd(),"ally_"+i+".txt"), 'a') as f:
                f.write("%s\n" % file1)

An other issue can be the week performance which you can have in case of many directories and files. In your code you run through the filenames in the directory for each extension and open the output file again and again. One more issue can be the checking of the extension. In most cases the extension can be determined by checking the ending of the file name, but sometimes it can be misleading. E.g. '.doc' is an extension however in a filename 'Medoc' the ending 'doc' is just 3 letters in a name. So I give an example solution for these problems:

import os

exts = ['ppt', 'pptx', 'doc', 'docx', 'txt', 'pdf', 'epub']
files = []
outfiles = {}
for root, dirnames, filenames in os.walk('.'):
        for filename in filenames:
            _, ext = os.path.splitext(filename)
            ext = ext[1:] # we do not need "."
            if ext in exts:
                file1 = os.path.join(root, filename)
                #print(i,file1)
                if ext not in outfiles:
                    outfiles[ext] = open(os.path.join(os.getcwd(),"ally_"+ext+".txt"), 'a')
                outfiles[ext].write("%s\n" % file1)
for ext,file in outfiles.iteritems():
    file.close()
quantummind
  • 2,086
  • 1
  • 14
  • 20