python


How to allow for multiple file names when searching through a directory in python?


How can I write something like:
module_path = os.path.join(root_dir,_dir,'Tests.py')
to be able to also take into considerations files named Test.py and test.py and tests.py?
If the file Tests.py isn't found, how can it check for another of the two to make sure that who ever wrote the program gets their file seen?
What I am trying to say is is there something that would allow me to do this?:
module_path = os.path.join(root_dir,_dir,'(T or t)est(s).py')
If it is literally those four files you are searching for then something like the following will work:
import os
_dir = 'test'
names=['Test.py','test.py','Tests.py','tests.py']
for fname in names:
test_path = os.path.join(_dir, fname)
if os.path.isfile(test_path):
module_path = test_path
break
You will need to modify to suit your environment.
You could use the glob module, which supports a simple version of regular expressions ("Unix-style wildcards"). E.g., you can use this:
for name in glob.glob("[tT]est*.py"):
print("I found", name)
This matches files whose name begins with "Test" or "test", and ends with .py. It won't give you the names in order of preference, but it might be all you need.

Related Links

Is there a way to quickly create many files many sequential files (ex1 . . . ex 50) in Python?
Pygame playing sound with pygame.mixer.music.load(file) gives NoneType error
How to update multiple values in Mongodb using pymongo?
Manually return an error result and status failure for a celery task?
Python backtrace from C++ embedded in Python
Find nth smallest element in numpy array [duplicate]
How to extract array from the first element of triples in 2d array of triples using numpy
Simulate user clicking in QSystemTrayIcon
Parsing GeoRSS feed with python scripting
User interface optimization when using pack
Selenium doesn't return after starting Chrome but returns after starting Firefox?
Python JSON decoding error TypeError: can't use a string pattern on a bytes-like object
Equivalent of \b word boundary in str.contains?
How to store django objects as session variables ( object is not JSON serializable)?
SQLAlchemy (w/ Postgres) - How to limit full-text search to one column of many indexed?
Split on separators not giving exact output

Categories

HOME
sidekiq
answer-set-programming
wmic
wildcard
puppet
actionscript
survival-analysis
appmaker
i2c
uiactivityviewcontroller
hosts
uiscrollview
symfony-forms
cratedb
typeahead
cross-platform
jquery-terminal
alfresco-share
derived
chocolatey
ejbca
kadanes-algorithm
jni
deb
countif
source-maps
widevine
i3
fileinfo
cortex-m3
configure
superagent
sharp
apache-cloudstack
pdflatex
pycparser
ocs
transient
lego
npm-publish
zero
fltk
catia
encapsulation
context-free-language
amazon-clouddrive
vcf
rhel6
workflow-foundation-4.5
gmt
sql-import-wizard
c#-interactive
instruments
pagekit
soda
gemini
axes
web-essentials
plone-3.x
selendroid
adobe-indesign
cpan
activity-streams
line-numbers
emma
resource-files
flash-cc
baucis
sim900
osx-snow-leopard
multiple-login
pep8
android-contextmenu
tridion2009
ajax.beginform
shared-objects
electronic-signature
coff
msbuildextensionpack
for-xml-path
nagle
rijndael
mvccontrib-grid
visual-c++-2008-express
virtualquery
configurable

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App