# Python, there a metacharacter for the statement to match anything?



## Hybrid_theory (May 15, 2010)

I want this python script to open a txt file which will likely have a changing timestamp as a name. so ultimately id like:

file= open('*.txt', 'r')

to work, but it doesnt, cause it looks at * as a character.

Any suggestions


----------



## Deleted member 3 (May 15, 2010)

I don't know any python, but considering wildcards can return multiple files I'm guessing file = open() doesn't handle multiple returns. So I would guess, read all files from the dir, loop through them and execute file open where extension is txt, or right, 3 is txt, whatever python allows.  Of course it could still screw up things if there are multiple files, depending on what you're doing.


----------



## Kreij (May 15, 2010)

Dan is right. The open command wants a Path+Filename to open a specific file.
You would have to itterate through the files in the directory and open them with their specific filenames (one at a time) based on some specific criteria.


----------



## Hybrid_theory (May 15, 2010)

Kreij said:


> Dan is right. The open command wants a Path+Filename to open a specific file.
> You would have to itterate through the files in the directory and open them with their specific filenames (one at a time) based on some specific criteria.



mmmmm

ive been experimenting with regular expressions as a string anyway to put that in there?


----------



## Kreij (May 16, 2010)

What you want to probably do is use the regular expression to see if you get a match (the file exists) and then call the Open function.If you try to put the test inside the Open function and it returns nothing, the call will throw an exception or something. (I'm not that familiar with Python, but it's similar to c# in that it's OO).


----------



## unibrow1990 (May 16, 2010)

To use regex in python you need to import the 're' module, and the '.' metacharacter is the one you want to use to match anything. 

However In this case I think you want to use the built in fnmatch module: http://docs.python.org/library/fnmatch.html


----------



## Hybrid_theory (May 16, 2010)

unibrow1990 said:


> To use regex in python you need to import the 're' module, and the '.' metacharacter is the one you want to use to match anything.
> 
> However In this case I think you want to use the built in fnmatch module: http://docs.python.org/library/fnmatch.html



yeah i was trying to do re.compile with a shutil.copy2 but it wasnt playing nice either.

i did:

source_file = re.compile(r'(\d\d)\-(\d\d)\-(\d\d\d\d)\.[tar]\.[gz]')

same idea with a destination variable.

then

shutil.copy2(source_file, destination)

but it wouldnt take the damn thing, it didnt like the code i was trying. did some variations with the brackets in there and such.

I tried similar stuff with the txt file to no avail, but i will check out this one you have suggested.


----------



## Hybrid_theory (May 17, 2010)

> To use regex in python you need to import the 're' module, and the '.' metacharacter is the one you want to use to match anything.
> 
> However In this case I think you want to use the built in fnmatch module: http://docs.python.org/library/fnmatch.html



This worked perfectly. thanks so much. script is a lot cleaner now too


----------

