-2

I have a folder with pdf files and I have a text file: helloworld.txt and another txt file description.txt

The helloworld.txt is empty by default. The description.txt contains the files' description. Every line one description.

I want that in every line in helloworld.txt contain the pdf files' names, files' sizes, and descriptions of the files.

So every helloworld.txt line looks like this: {filename} {filesize} {description}

So many lines like pdf elements in the folder.

I have got the following code snippet:

import os
import glob


textfilename = 'helloworld.txt'


descriptiontext = open("description.txt", 'r')
with open(textfilename, 'a') as textfile:  # Open the text file for appending
for filename in glob.iglob('*.pdf'):  # For every file in the current directory matching '*.pdf'
    stat = os.stat(filename)  # os.stat gets various file statistics
    filesize = stat.st_size/1024/1024
    filesize = round(filesize,2)
    description = descriptiontext.readline()
    textfile.write(f'{filename}   {filesize}   {description} \n')  # \n means newline

The script is working almost perfectly. The {filename}{filesize}{description} at the good place.

The problem: the pdf folder set to sort by modification time (how I downloaded from a site) and it looks like in the folder (Lubuntu 20.04 LTS) but after I run the script the {filename} sequence is not the same as the folder's sequence in the helloworld.txt file.

How to modify the code to write the {filename} at the same sequence in the helloworld.txt like in the folder sequence sort by modification time?

3 Answers3

0
import os

directory = '/home/user/Documents/'

with open("hello.txt", "a") as f: 
    for file in os.listdir(directory):
        if file.endswith(".pdf"):
            size = os.path.getsize(directory + file)
            f.write(str(size))
Snow
  • 1,058
  • 2
  • 19
  • 47
0
import os
with open(textfile,'a') as f:
    for item in os.listdir(os.path.abspath(os.curdir)):
        if item.endswith('.pdf'):
            f.write(str(os.path.getsize(item))
Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
0

Firstly, since you asked for a simple solution, I'd point out that if you are using anything that resembles a Linux shell, this can just be done at the command line, like so:

$ ls -al
total 5968
drwxr-xr-x   5 edwsmith  staff      160 May 20 10:01 .
drwxr-xr-x  37 edwsmith  staff     1184 May 20 09:56 ..
-rw-r--r--   1 edwsmith  staff  1024000 May 20 09:57 1.pdf
-rw-r--r--   1 edwsmith  staff  2024000 May 20 09:57 2.pdf
-rw-r--r--   1 edwsmith  staff       39 May 20 10:01 textfile.txt

$ cat textfile.txt
this is some existing text in the file

$ ls -l *.pdf | cut -d ' ' -f 8,12 >> textfile.txt

$ cat textfile.txt
this is some existing text in the file
1024000 1.pdf
2024000 2.pdf

Doing this in python is a bit more work, but not much:

import os
import glob

textfilename = 'textfilename'

with open(textfilename, 'a') as textfile:  # Open the text file for appending
    for filename in glob.iglob('*.pdf'):  # For every file in the current directory matching '*.pdf'
        stat = os.stat(filename)  # os.stat gets various file statistics
        filesize = stat.st_size
        textfile.write(f'File {filename} has size {filesize} bytes\n')  # \n means newline
Cargo23
  • 3,064
  • 16
  • 25