0
votes

Extraire chaque année, mois, jour, année de GetCTime, getmtime à Python

Je veux extraire l'année des heures de jour Min min même de la valeur inférieure de la valeur inférieure.

import os, time, os.path, datetime

date_of_created = time.ctime(os.path.getctime(folderName))
date_of_modi = time.ctime(os.path.getmtime(folderName))


0 commentaires

4 Réponses :


0
votes
 time.ctime([secs])

0 commentaires

0
votes

Vous pouvez utiliser le module DateTime, plus spécifiquement de la fonction fromtalestamp () code> du module DateTime pour obtenir ce que vous attendez.

import os, time, os.path, datetime

date_of_created = datetime.datetime.fromtimestamp(os.path.getctime(my_repo))
date_of_modi = datetime.datetime.fromtimestamp(os.path.getmtime(my_repo))

print(date_of_created.strftime("%Y"))


2 commentaires

J'ai essayé mais j'ai reçu AttributeError: "str 'objet n'a pas d'attribut" StrfTime "ce message


datetime.datetime.fromTimeStamp () renvoie un objet DateTime. Vérifiez le code que vous avez appliqué et celui de cette solution. Vous convertissez sûrement votre dateTime en une chaîne quelque part.



1
votes

Vous pouvez convertir la chaîne en un objet DateTime:

from datetime import datetime
date_of_created = datetime.strptime(time.ctime(os.path.getctime(folderName)), "%a %b %d %H:%M:%S %Y") # Convert string to date format
print("Date created year: {} , month: {} , day: {}".format(str(date_of_created.year),str(date_of_created.month),str(date_of_created.day)))


0 commentaires

0
votes

the TIME.CTIME CODE> A> La fonction renvoie l'heure locale dans String code>. Vous voudrez peut-être utiliser le TIME.LOCALTEMENT CODE> fonction, qui renvoie un structure_time code > objet qui contient les informations que vous recherchez. A l'exemple,

import os, time

date_created_string = time.ctime(os.path.getctime('/home/b-fg/Downloads'))
date_created_obj = time.localtime(os.path.getctime('/home/b-fg/Downloads'))
print(date_created_string) # Mon Feb 10 09:41:03 2020
print('Year: {:4d}'.format(date_created_obj.tm_year)) # Year: 2020
print('Month: {:2d}'.format(date_created_obj.tm_mon)) # Month:  2
print('Day: {:2d}'.format(date_created_obj.tm_mday)) # Day: 10


0 commentaires