2

I am new to python.

I was trying to write a program that will read files from a .txt file.

(that means I have a 'filenames.txt' file and have filenames with their paths in that file) How can I read those file names from that .txt file and get the date the file was created?

Heres the code I came up with:

import sys, os
import pathlib

# list of filenames with their paths separated by comma 
file_list = []  

# input file name which contains list of files separated by \n
with open ('filenames.txt' , 'r+' ) as f :
    list_file = f.readlines().splitlines()

input_list = file_list + list_file  

def file_check(input_list):
    if input_list is none:
      print ("input_list is null")

print (input_list)

Thanks in advance.

TYZ
  • 8,466
  • 5
  • 29
  • 60
kishore ezio
  • 23
  • 1
  • 4
  • Need a little more information, what sort of files are listed in that text file? What do you mean by "read"? If they – Capn Jack Oct 02 '18 at 18:30
  • You can read [here](https://www.pythonforbeginners.com/cheatsheet/python-file-handling), try to code yourself and ask if you find any errors or unexpected outputs, given no one else have asked a similar question. – Irfanuddin Oct 02 '18 at 18:31
  • @CapnJack I have a filenames.txt file that contains bunch of files like follows. c/users/......... file1.txt c/users/......... file2.txt c/users/......... file3.txt I want my program to read those files using those file paths and return the number of lines in each file. – kishore ezio Oct 02 '18 at 18:35
  • Sorry got a call while I was trying to edit my accidentally submitted original comment so I can't fix it now. But I mean to ask: can I assume all the files in this text file are other text files? – Capn Jack Oct 02 '18 at 18:37
  • Yes, All files within the filenames.txt are text files. – kishore ezio Oct 02 '18 at 18:39

3 Answers3

4

by this you can open a file:

file = open('path/to/filenames.txt')

assuming data is written one file name per line you can read from your file like this:

filename = file.readline()

then for knowing the time of creation you can import os and use stat function. this function will tell you st_atime which is last accessed time, st_mtime as last modified time and st_ctime as creation time. take a look at here :

import os
stat = os.stat(filename)
creation_time = stat.s_ctime

for ommiting whitespaces at the end of the filenames you can use rstip. So, altogether it will look like this:

import os
file = open('path/to/filenames.txt')
filename = file.readline()
while filename:
    stat = os.stat(filename.rstrip())
    creation_time = stat.st_ctime
    print(creation_time)
    filename = file.readline()
Shahin Shemshian
  • 356
  • 1
  • 11
  • I really appreciate your help But this is the output coming with your code. 1538435352.7896326 1538176072.1286607 1538494272.3550172 – kishore ezio Oct 02 '18 at 18:55
  • 1
    if you want to make the time human readable, you should format it. take a look at the solution here: https://stackoverflow.com/questions/39359245/from-stat-st-mtime-to-datetime – Shahin Shemshian Oct 02 '18 at 18:59
0

If they are in this format: [filename] [path] on each line i suggest the following:

f = open('filenames.txt', 'r').read().splitlines()

This will read from the file and then split it into lines

f = [x.split(' ') for x in f]

It is a shorten way of iterating over f which is a list of string and then split each string at the space so it will be [filename, path]

Here things get a little bit complicated:

import os
from datetime import datetime
from time import strftime
datetime.fromtimestamp(os.path.getctime('filenames.txt')).strftime('%Y-%m-%d %H:%M:%S')

All the modules used are builtin

Good Luck

Andrew
  • 11
  • 6
0

You can check file creation time by using:

import os, time
time.ctime(os.path.getctime('your_full_file_path'))
mwen1993
  • 63
  • 4