Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file size, creation date and modification date in Python

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

like image 277
Nira Avatar asked Jun 08 '26 08:06

Nira


2 Answers

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)

like image 130
YamiOmar88 Avatar answered Jun 10 '26 22:06

YamiOmar88


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$
like image 41
learning2learn Avatar answered Jun 10 '26 23:06

learning2learn