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.

4
  • possible duplicate of regular expression using in glob.glob of python Commented Mar 16, 2015 at 11:51
  • For simple cases like this, glob.glob is much shorter :) Commented Mar 16, 2015 at 13:25
  • This doesn't look like a duplicate of regular expression using in glob.glob of python. 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. Commented Mar 16, 2015 at 14:46
  • In the end, I used glob.iglob, based on this [link] (stackoverflow.com/questions/225735/…) 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 Commented Mar 17, 2015 at 11:09

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.