-2
import os
s = os.listdir("qwe")
f = open("asd.txt", "w")
for i in range(0, 100):
    try:
        f.writelines(s[i] + ":" + "\n")
        f.writelines(os.listdir("qwe\ ".strip() + s[i] + "\Wallets"))
        f.writelines("\n" + "\n")
    except:
        continue

It prints data like this: dsadasda: ada.txtli.pysda.txt

elele: erti: file.txt

jhgjghjgh: new.txtpy.py

lolo: sdada: If there are lots of things in wallet it prints them together, how can i space between them?

  • You are opening your file in write mode, which truncates it, in each loop, so you'll only get the content written during the last one. Also, if anything goes wrong, your bare `except` will hide it... – Thierry Lathuille Sep 07 '22 at 11:45
  • Luka just said that it prints the data, so the try-except is not the problem. – Nima Sep 07 '22 at 11:49

1 Answers1

0

you may write your code as this way

import os
s = os.listdir("qwe")
try:
    with open("asd.txt", "a") as f: # opening file once 
        for i in range(0, 100):
            f.writelines(s[i] + ":" + "\n")
            print(s[i] + ":" + "\n")
            f.writelines(os.listdir("qwe\ ".strip() + s[i] + "\Wallets"))
            print(os.listdir("qwe\ ".strip() + s[i] + "\Wallets"))
            f.writelines("\n" + "\n")
except:
    pass

this is happening because you open the file for each loop so it doesn't write anything in your file , another thing that you are opening file in writing mode which means that it will erase the content of the file and replace it with the new one

Ali fareeq
  • 43
  • 6