There is a working directory where I downloaded 7 PDF files. The files are sorted by modification time so the order is: (file1.pdf, file2.pdf .. file7.pdf).
There is a text file (description.txt) that contains 7 lines.
Like this:
description1
description2
.
.
description7
I have a text file (helloworld.txt) what I want to append every line with {filename} {filesize} {description} like this:
{file1.pdf} {filesize of file1.pdf} {description1}
{file2.pdf} {filesize of file2.pdf} {description2}
.
.
{file7.pdf} {filesize of file7.pdf} {description7}
What I have tried:
I have written a script in Python:
import os
import glob
textfilename = 'helloworld.txt'
descriptiontext = open("description.txt", 'r')
with open(textfilename, 'a') as textfile:
for filename in glob.iglob('*.pdf'):
stat = os.stat(filename)
filesize = stat.st_size/1024/1024
filesize = round(filesize,2)
description = descriptiontext.readline()
textfile.write(f'{filename} {filesize} {description} \n')
I have run the code but the helloworld.txt not correct. The result is the following:
file1.pdf 0.08 description1
file6.pdf 0.21 description2
file3.pdf 0.14 description3
file2.pdf 0.02 description4
file5.pdf 0.14 description5
file7.pdf 0.15 description6
file4.pdf 0.2 description7
The filename order is not correct (conjugated with file size). The order is not the same as in the folder order (download order). (I did not change it. It sort by modification time.)
My OS is: Linux Lubuntu 20.04LTS
Can somebody solve this problem?