[Tutor] Tutor Digest, Vol 134, Issue 27 - Re: annoying directory structure (Zachary Ware)

Dave Angel davea at davea.name
Fri Apr 10 13:39:44 CEST 2015


Please at least fix the subject line.  Thanks for deleting the 
irrelevant parts of the digest, but you're pretending to continue a 
thread, while having a bogus subject, and sending a message that's not 
at all linked to the previous part of the thread.

When I used to use a digest, I was able to open a particular message 
within the digest (I think the individual messages were considered 
attachments, and I just double-clicked on one such attachment).  Then i 
could reply, and it actually worked.  Your mileage may vary, which is 
why it's better to just skip digests when you're actually participating.


On 04/09/2015 11:07 PM, Jim Mooney wrote:
>>
>> I'll need to see some actual code (namely, where and how you used
>> os.walk) to have any real idea of what's going on. The online docs for
>> os.walk have a pretty good explanation and example.
>
>
> Zach
>
> This worked. I didn't know why I got doubled strings, but print
> concatenated them so I printed to a file, and the text lines all open an
> mp4 in windows explorer, as expected. Unfortunately they aren't in order,
> so I guess I'll have to actually read up on walk. I just wanted to knock it
> out quickly by figuring out the walk output format, and get on with the
> course ;')
>
> tutfiles = open('tutfiles.txt', 'w')
> p = os.walk('I:\\VIDS\\Learn Python Track\\')
> for triplet in p:
>      if triplet[2] == []:
>          continue
>      else:
>          if 'mp4' in triplet[2][0]:

If you're looking for an extension of mp4, you should either use 
endswith, or use os.path.splitext to get the actual extension.  The way 
you have it now, you'll also tag a file that happens to have those 3 
letters somewhere in the middle.

The other thing weird in this code is you're only looking at the first 
file in the directory.  If the file you're interested in happens to be 
the fifth, you'll never see it.  Normally, you need to loop over triplet[2]



>              print(triplet[0] + '\\' + triplet[2][0], file=tutfiles)
>
> tutfiles.close()
>
> Here are the out of order textlines, going from Track\08 to Track\01

If you want the directory names to be sorted, and your OS doesn't happen 
to sort them, you must do it yourself.  Simply sort triplet[1] each 
time.  If you also want the filenames within a directory to be sorted, 
then sort them as well.  These two sorts would take place just before 
your "if triple3t[2]" above.

>
> I:\VIDS\Learn Python Track\08. Using Databases in Python\Stage 1\01. Meet
> Peewee\01. Meet Peewee, Our ORM\Meet Peewee, Our ORM.mp4
> I:\VIDS\Learn Python Track\01. Python Basics\Python Basics\Stage 6\06.
> Pick a Number! Any Number!\02. The Solution\The Solution.mp4
>
> Here is what I meant by doubled strings. The tuples output by walk had two
> strings in a row as element zero, Prior to the first comma, with element 1
> being an empty list, so I couldn't figure how to concatenate the two
> strings directly, without print, to use in my file listing.

No idea what this paragraph is trying to say.  But the first element of 
the tuple is a string, so it cannot have two items in it.  If it seems 
to, perhaps you have some unusual filenames, like the asterisk and 
parenthesis in the following thingie.

>
> (*'I:\\VIDS\\Learn Python Track\\07. Regular Expressions in Python\\Stage '
> '1\\01. Introduction to Regular Expressions\\07. Compiling and Loops'*, [],
> ['Compiling and Loops.mp4', 'Compiling and Loops.srt'])
>

If we're going to make any sense of that mess, you need to show the code 
that produced it.

-- 
DaveA


More information about the Tutor mailing list