How to space items?

Viewed 28
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?

1 Answers

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

Related