6

I need to get file info (path, size, dates, etc) and save it in a txt but I don't know where or how to do it.

This is what I have:

ruta = "FolderPath"
os.listdir(path=ruta)
miArchivo = open("TxtPath","w")
def getListOfFiles(ruta):
 listOfFile = os.listdir(ruta)
 allFiles = list()
 for entry in listOfFile:
  fullPath = os.path.join(ruta, entry)
  if os.path.isdir(fullPath):
   allFiles = allFiles + getListOfFiles(fullPath)
  else:
   allFiles.append(fullPath)
 return allFiles

listOfFiles = getListOfFiles(ruta)
for elem in listOfFiles:
 print(elem)
 print("\n")
 miArchivo.write("%s\n" % (elem))
miArchivo.close()

The output is (only path, no other info): enter image description here

What I want is: V:\1111111\222222222\333333333\444444444\5555555555\66666666\Folder\File name -- size -- modification date and so on

Nira
  • 197
  • 1
  • 1
  • 12

3 Answers3

11

I think that you may want to use scandir instead of listdir for this:

for item in os.scandir(my_path):
     print(item.name, item.path, item.stat().st_size, item.stat().st_atime)

You will also want to check here for more detailed information regarding the appropriate calls (for the time you are looking for and the size). (os.scandir was added in python 3.6)

Ribo
  • 3,363
  • 1
  • 29
  • 35
YamiOmar88
  • 1,336
  • 1
  • 8
  • 20
  • Where should I put this? Sorry, I'm new and I'm very lost – Nira Mar 03 '20 at 12:20
  • 1
    @Nira, you could fit this into the rewrite I gave in my answer. I had upvoted this one as it is so succinct. Basically you could return the items in a tuple, then use the write code to print it to your output file. – learning2learn Mar 03 '20 at 17:19
4

https://docs.python.org/2.7/library/os.path.html#module-os.path

os.path.getsize(path) # size in bytes
os.path.ctime(path) # time of last metadata change; it's a bit OS specific.

Here's a rewrite of your program. I did this:

  1. Reformatted with autopep8 for better readability. (That's something you can install to prettify your code your code. But IDEs such as PyCharm Community Edition can help you to do the same, in addition to helping you with code completion and a GUI debugger.)
  2. Made your getListofFiles() return a list of tuples. There are three elements in each one; the filename, the size, and the timestamp of the file, which appears to be what's known as an epoch time (time in seconds since 1970; you will have to go through python documentation on dates and times).
  3. The tuples is written to your text file in a .csv style format (but note there are modules to do the same in a much better way).

Rewritten code:

import os

def getListOfFiles(ruta):
    listOfFile = os.listdir(ruta)
    allFiles = list()
    for entry in listOfFile:
        fullPath = os.path.join(ruta, entry)
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            print('getting size of fullPath: ' + fullPath)
            size = os.path.getsize(fullPath)
            ctime = os.path.getctime(fullPath)
            item = (fullPath, size, ctime)
            allFiles.append(item)
    return allFiles

ruta = "FolderPath"
miArchivo = open("TxtPath", "w")
listOfFiles = getListOfFiles(ruta)
for elem in listOfFiles:
    miArchivo.write("%s,%s,%s\n" % (elem[0], elem[1], elem[2]))
miArchivo.close()

Now it does this.

my-MBP:verynew macbookuser$ python verynew.py; cat TxtPath
getting size of fullPath: FolderPath/dir2/file2
getting size of fullPath: FolderPath/dir2/file1
getting size of fullPath: FolderPath/dir1/file1
FolderPath/dir2/file2,3,1583242888.4
FolderPath/dir2/file1,1,1583242490.17
FolderPath/dir1/file1,1,1583242490.17
my-MBP:verynew macbookuser$
learning2learn
  • 405
  • 5
  • 11
  • +1, quite useful, thanks. I could see the merit of writing the next to the last line as `miArchivo.write("%s,%s,%s\n" % elem)` – gt6989b Aug 01 '22 at 20:56
  • Also using `os.stat()` for one stand-alone file and `os.scandir()` for a directory would make a lot of sense, instead of having 2 system calls for every file... – gt6989b Aug 01 '22 at 21:15
0

To interpret the dates, use https://stackoverflow.com/a/52858040/11262633. Building on YamiOmar88's great answer:

import os
import datetime

def ts_to_dt(ts):
    return datetime.datetime.fromtimestamp(ts)

for item in os.scandir(my_path):
     print(item.name, item.path, item.stat().st_size, ts_to_dt(item.stat().st_atime))
mherzog
  • 1,085
  • 1
  • 12
  • 24