G
G
getrighton2021-08-22 22:24:56
Python
getrighton, 2021-08-22 22:24:56

How to save many lines in os?

I wrote a small script that should copy the data from tokens.txt (there are a lot of tokens.txt folders, so I put everything in a loop) to done.txt, but the line break does not help. What can be done?

import os
path_to = ('E:/sorter ds/logs')
list = os.listdir(path_to)
number = (len(list))

for i in range(number):

    with open(path_to + '/' + list[i] + '/Discord/Tokens.txt','r') as f1:
        data = f1.readlines()

    with open("done.txt",'w') as f2:
        for line in data:
            f2.write(line + '\n')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-08-23
@phaggi

First, do not use the names of regular types and functions as variables (for example, list). Instead, if you really want to, write my_list or list_of_path
Secondly, it is more convenient to use the pathlib library for working with paths (better even pathlib2)
Thirdly, instead of using for item in range(len(my_list)) it is better to use the Well and , fourthly, you overwrite the done.txt file every time, since you specified “w”. This option overwrites an existing file. You need to create this file once, and then use the "a" (append) option to add newlines to the existing one. Ps The "a" parameter also creates the file itself if it doesn't already exist. for i, value in enumerate(my_list)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question