[Tutor] select particular directories and files

Andre' Walker-Loud walksloud at gmail.com
Tue Aug 30 05:40:02 CEST 2011


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 <walksloud at 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 <emile at 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 at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> 
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110829/245e77ae/attachment-0001.html>


More information about the Tutor mailing list