to have multiple dirs is simple,

> for dir in glob.glob(MainFolder + '*/01/') + glob.glob(MainFolder + '*/02/') 
> + glob.glob(MainFolder + '*/03/'):


there may be a better way, but this should work.


By the way, not to discourage you from asking questions on the list, but many 
of these things can be deduced quickly by trial and error using the python 
interpreter (the interactive python shell).  That is one thing I really like 
about python, you really can just play around with it.  And with all things, if 
you figure it out on your own, then you will likely feel better about that, and 
also, you will retain the knowledge better and gain more confidence in trying 
new things.


Cheers,

Andre





On Aug 29, 2011, at 9:18 PM, questions anon wrote:

> It worked! thank you. This is the code I ended with:
> 
> for dir in glob.glob(MainFolder + '*/01/'):
>         print dir
>         for ncfile in glob.glob(dir + '*.nc'):
>                 print ncfile
> 
> can you choose more than one folder with glob?
> i.e. I tried:
> for dir in glob.glob(MainFolder + '*/01/', '*/02/', '*/03/'):
> but I received:
> TypeError: glob() takes exactly 1 argument (3 given)
> 
> 
> thanks
> 
> 
> On Tue, Aug 30, 2011 at 1:40 PM, Andre' Walker-Loud <walksl...@gmail.com> 
> wrote:
> hello,
> 
> yes, I would also try adding a wild card in the dir search
> 
>>> for dir in glob.glob(MainFolder + '01\*'):
> 
> 
> to check if this is helps, in an interpreter (rather than script) try
> 
> dirs = glob.glob(MainFolder + '\01\*'):
> print dirs
> 
> 
> if you get "[]" then this was not the answer, but if you get a list of 
> directories, then this should work.
> 
> Well, it should work with the correction
> 
> 
>>>         for ncfile in glob.glob(dir+'\*.nc'):
> 
> 
> 
> Cheers,
> 
> Andre
> 
> 
> 
> 
> 
> 
> On Aug 29, 2011, at 8:35 PM, questions anon wrote:
> 
>> thanks, that was an error by me. but that still doesn't help me select the 
>> dir and files! 
>> Could it be because I am trying to select folders within other folders to 
>> then get a file from each of those folders?
>> 
>> 
>> On Tue, Aug 30, 2011 at 1:30 PM, Andre' Walker-Loud <walksl...@gmail.com> 
>> wrote:
>> Dear Anonymous Questioner,
>> 
>> I am not sure how the Windows environment works, but in linux, I would 
>> replace
>> 
>>>         for ncfile in glob.glob('.nc'):
>> 
>> 
>> with 
>> 
>>>         for ncfile in glob.glob('*.nc'):
>> 
>> 
>> ie, add the "wild card" '*' character to grab all files which end in '.nc'
>> 
>> 
>> Andre
>> 
>> 
>> 
>> 
>> On Aug 29, 2011, at 7:23 PM, questions anon wrote:
>> 
>>> Thanks for responding
>>> When I try glob.glob I receive no errors but nothing prints.
>>> 
>>> MainFolder=r"E:/Sample/"
>>> for dir in glob.glob(MainFolder + '01'):
>>>         print "my selected directories are:", dir
>>>         for ncfile in glob.glob('.nc'):
>>>             print "my selected netcdf files are:", ncfile
>>> 
>>> any suggestions? thanks
>>> 
>>> 
>>> On Tue, Aug 30, 2011 at 10:07 AM, Emile van Sebille <em...@fenx.com> wrote:
>>> On 8/29/2011 4:52 PM questions anon said...
>>> 
>>> I am trying to select particular files within
>>> a particular subdirectory,
>>> 
>>> You might find glob a better starting point:
>>> 
>>> ActivePython 2.6.6.15 (ActiveState Software Inc.) based on
>>> Python 2.6.6 (r266:84292, Aug 24 2010, 16:01:11) [MSC v.1500 32 bit 
>>> (Intel)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>> >>> import glob
>>> >>> help(glob.glob)
>>> Help on function glob in module glob:
>>> 
>>> glob(pathname)
>>>    Return a list of paths matching a pathname pattern.
>>> 
>>>    The pattern may contain simple shell-style wildcards a la fnmatch.
>>> 
>>> >>> for filename in glob.glob(r'C:\WSG\GL\2011-08\*.txt'):
>>>    print filename
>>> ...
>>> C:\WSG\GL\2011-08\2011-01-WIP-Details.txt
>>> C:\WSG\GL\2011-08\2011-02-WIP-Details.txt
>>> C:\WSG\GL\2011-08\2011-03-WIP-Details.txt
>>> C:\WSG\GL\2011-08\2011-04-WIP-Details.txt
>>> C:\WSG\GL\2011-08\2011-05-WIP-Details.txt
>>> C:\WSG\GL\2011-08\2011-06-WIP-Details.txt
>>> C:\WSG\GL\2011-08\2011-07 - bankToRec.txt
>>> C:\WSG\GL\2011-08\2011-07 - vsdsToRec.txt
>>> C:\WSG\GL\2011-08\2011-07-WIP-Details.txt
>>> C:\WSG\GL\2011-08\5790-00 RECONCILIATION.txt
>>> C:\WSG\GL\2011-08\BankRecUtils.txt
>>> C:\WSG\GL\2011-08\CapitalizationExamples.txt
>>> C:\WSG\GL\2011-08\DEALLOCATE-2011-04.txt
>>> C:\WSG\GL\2011-08\dump glsmf1 data for 2004-2010.txt
>>> C:\WSG\GL\2011-08\MAR DEALLOCATION.txt
>>> C:\WSG\GL\2011-08\Notes.txt
>>> C:\WSG\GL\2011-08\shipping safety net util.txt
>>> C:\WSG\GL\2011-08\UNBILLED WIP.txt
>>> C:\WSG\GL\2011-08\Vacation Accrual - post-bonus-changes.txt
>>> C:\WSG\GL\2011-08\Vacation Accrual - pre-bonus-changes.txt
>>> C:\WSG\GL\2011-08\vacation accrual notes.txt
>>> >>>
>>> 
>>> 
>>> 
>>> 
>>> I have been able to do both but not together!
>>> When I try to select my files within the dir loop nothing comes up, but
>>> when I leave the files outside the dir loops it selects all the files
>>> not just the ones in the dirs I have selected.
>>> The code I am using is:
>>> 
>>> import os
>>> 
>>> MainFolder=r"D:/samples/"
>>> 
>>> for (path, dirs, files) in os.walk(MainFolder):
>>>     for dir in dirs:
>>>         if dir=='01':
>>>             print "selected directories are:",dir
>>> 
>>>             for ncfile in dir:
>>>                   if ncfile[-3:]=='.nc':
>>>                         print "ncfiles are:", ncfile
>>> 
>>> Any feedback will be greatly appreciated!!
>>> 
>>> 
>>> _______________________________________________
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>> 
>>> 
>>> _______________________________________________
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>> 
>>> _______________________________________________
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> 
> 
> 

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to