0

I am trying to rename a file in which it is auto-generated by some local modules but I was wondering if using os.listdir is the only way for me to filter/ narrow down this file. This file will always be generated before it is removed and the code will generate the next one (still in the same directory) based on the next item in list.

Basically, whenever this file is generated, it comes in the following file path: /user_data/.tmp/tempLibFiles/2015_03_16_192212_182096.con

I had only wanted to rename the 2015_03_16_192212_182096 into connectionFile while keeping the rest the same.

yan
  • 631
  • 9
  • 30
  • possible duplicate of [regular expression using in glob.glob of python](http://stackoverflow.com/questions/13031989/regular-expression-using-in-glob-glob-of-python) – Peter Wood Mar 16 '15 at 11:51
  • For simple cases like this, `glob.glob` is much shorter :) – Dima Tisnek Mar 16 '15 at 13:25
  • This doesn't look like a duplicate of [regular expression using in glob.glob of python](http://stackoverflow.com/q/13031989/1600898). That question is specifically about using regexps in `glob.glob`, while in this question, the OP was apparently unaware of `glob.glob` in the first place, and the pattern seems simple enough to be matchable with simple shell-style globbing patterns. – user4815162342 Mar 16 '15 at 14:46
  • In the end, I used `glob.iglob`, based on this [link] (http://stackoverflow.com/questions/225735/batch-renaming-of-files-in-a-directory]) first solution. Sadly there still seems to be some problem as I am unable to grab the 'output' of the renamed convention unless I am going to do another round of finding/ narrowing the file within the directory – yan Mar 17 '15 at 11:09

1 Answers1

1

You can also use the glob module to narrow down the list of files to the one that matches a particular pattern. For example:

import glob
files = glob.glob('/user_data/.tmp/tempLibFiles/*.con')
user4815162342
  • 141,790
  • 18
  • 296
  • 355