-3

I am very new to python. I want to read files based on file name and not data type. Say I have Hello.txt_1, Hello.txt_2, Hello.txt_3 in a folder and these files are created automatically by an external code with Hello.txt_3 being the latest file. Now, I want to read the latest created file Hello.txt_3 and check for its contents. How is to be done in python ? I have figured out for files with common data type but not for common file name.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2269123
  • 15
  • 1
  • 4

2 Answers2

1

Use glob to perform wildcard matching. The following example will find all the files named as you state and last_file will contain the name of the latest by creation time (or None if no files were found).

import glob
import os

last_file = None
time=0
for i in glob.glob("Hello.txt_*"):
    if os.path.getctime(i) > time: 
        last_file = i

PS: This question is at the very beginner level however and should have been easily solved by googling.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Bas Jansen
  • 3,273
  • 5
  • 30
  • 66
  • Thanks a lot. When I edited your code as following, it correctly gave me the last created file. – user2269123 Apr 11 '13 at 08:48
  • import glob import os last_file = None time=0 for i in glob.glob("Hello.txt_*"): if os.path.getctime(i) > time: last_file = i time= last_file print last_file – user2269123 Apr 11 '13 at 08:52
  • @Jon Clements : Thanks a lot. I have achieved my objective with further adding these lines – user2269123 Apr 11 '13 at 09:43
  • @Jon Clements: import glob import os last_file = None time=0 for i in glob.glob("Hello.txt_*"): if os.path.getctime(i) > time: last_file = i time= last_file print last_file f = open(last_file, 'r') q= f.read() print q – user2269123 Apr 11 '13 at 09:43
0

From my understanding of the question, you probably want to ignore the file types as you cannot depend on them to tell you what you want to know. If the file type contains a number/alpha-numeric, 'sorted' will sort it in reverse order. Then you can simple read the first:

#!/usr/bin/python
from os import listdir
from os.path import isfile, join

#this is where you declare you directory
directory_2_look = '/var/tmp/lee/'

#This creates a list of the contents of your directory
files = [ f for f in listdir(directory_2_look) if isfile(join(directory_2_look,f)) ]

#this sorts the files for you in reverse order
files = sorted(files, reverse=True)
print files[0]

#this allows you to read the last file and print it out. If you want to write to it simply change the 'r'
file_to_read = open(join(directory_2_look,files[0]), 'r')

print file_to_read.read()

Results will look a bit like this:

['script.py', 'file.txt_99', 'file.txt_4', 'file.txt_3', 'file.txt_22', 'file.txt_21', 'file.txt_2', 'file.txt_1', 'file.txt_0'] script.py

!/usr/bin/python from os import listdir from os.path import isfile, join directory_2_look = '/var/tmp/lee/' files = [ f for f in

listdir(directory_2_look) if isfile(join(directory_2_look,f)) ] print sorted(files, reverse=True) files = sorted(files, reverse=True) print files[0] file_to_read = open(join(directory_2_look,files[0]), 'r')

print file_to_read.read()

Community
  • 1
  • 1
LeeO
  • 71
  • 3
  • 9
  • Thanks a lot. When I check the this part, I get the same python program created on the folder being and being printed. In the example you have given, is it possible to this operation only for file.txt and I guess you should get file.txt_99 as the output rather than script.py or any other files. – user2269123 Apr 11 '13 at 09:10
  • Yes it is, you simply need to create you script in another directory :-) Alternatively, look at files[1]. It gives you the second as the first is your script. However, this is dangerous, say you create another script in directory call script.py1, you will need to look at files[2] and so on. It is best not to create scripts in directory that contains your data. I only put it in there because I wanted you to see some content. All the other files were empty! :-) – LeeO Apr 11 '13 at 09:29
  • Thanks :-) But I need to do the exact operation but for files beginning only with specific name like "file.txt_" because in my case there are also other files created along with Hello.txt_ like Error.txt_ etc. Is it possible the same operation but only for the files of our interest ? – user2269123 Apr 11 '13 at 10:03