-2

this is code that I have written so far, it is creating the txt file but write just one name from the files in that folder. Print statements are working fine they are prinitng all the files

import os
path = 'C:/Users/XXX'
files = os.listdir(path)

for f in files:
    print(f)
    zip_file_name = os.path.basename(f).split(".")[0]
    print(zip_file_name)
    fp = open("write_demo.txt", 'w')
    print('Done Writing')
    fp.write(zip_file_name)
    fp.write("\n")
fp.close()
  • 1
    The way you are opening the file, the code throws away the old contents of the file. You likely want to use `'a'` instead of '`'w'` to append onto the existing file contents each time through your loop. Even better, open the file with `'w'` just once outside of the loop.. – CryptoFool Dec 06 '22 at 23:16
  • You might consider `print(zip_file_name, file=fp)` to avoid the need to add a newline. – Tim Roberts Dec 06 '22 at 23:18
  • @TimRoberts - I said that too ;) – CryptoFool Dec 06 '22 at 23:19
  • You can also do this from a command line with `"dir /b C:\Users\XXXX > write_demo.txt"`. – Tim Roberts Dec 06 '22 at 23:21
  • open the file with 'w' just once outside of the loop worked.. thank you – Tushar Singh Dec 06 '22 at 23:29
  • Now I am seeing they are not copying in the file in the same sequence as they are listed in the folder. What can be the reason for that? – Tushar Singh Dec 06 '22 at 23:34

2 Answers2

0

As @CryptoFool stated, you are opening the file in write mode. You need append mode. See the manual for details.

import os
path = 'C:/Users/XXX'
files = os.listdir(path)

for f in files:
    print(f)
    zip_file_name = os.path.basename(f).split(".")[0]
    print(zip_file_name)
    fp = open("write_demo.txt", 'a')
    print('Done Writing')
    fp.write(zip_file_name)
    fp.write("\n")
fp.close()
str1ng
  • 485
  • 3
  • 14
  • You are opening the same file many times without closing it. And anyway, there is really no reason to open it more than once... Just open it once for writing ***before*** the loop – Tomerikoo Dec 06 '22 at 23:35
  • Yes, that exactly what I did. I moved the open statement fp = open("write_demo.txt", 'w') outside the loop. – Tushar Singh Dec 06 '22 at 23:38
-1

Here's an example that brings together what is said in the comments, and also demonstrates how to use a with such that you can be sure that the file gets closed properly. Using with is the much preferred way to close files. Most notably, this will insure that the file is closed properly even when an exception is thrown inside the with block.

import os
path = 'C:/Users/XXX'
files = os.listdir(path)

with open("write_demo.txt", 'w') as fp:
    for f in files:
        print(f)
        zip_file_name = os.path.basename(f).split(".")[0]
        print(zip_file_name)
        fp.write(zip_file_name)
        fp.write("\n")
    print('Done Writing')

Note that f will contain only each file's name, not the full path to the file. To process the file, you'd want to compute its full path with os.path.join(path, f). The fact that you are calling basename on f suggests that you think it contains the full path. Also, the way you are taking off the extension won't work if the file 's name contains multiple . characters. A better way is zip_file_name, _ = os.path.splitext(f).

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • yeah I just need the file name, not the full path as I have path mentioned in path variable. Do you still think I should use os.path.join – Tushar Singh Dec 06 '22 at 23:48