Determining if a filename is greater than X characters
Bengt Richter
bokr at oz.net
Sun Aug 3 15:59:10 EDT 2003
On Sun, 03 Aug 2003 02:45:31 GMT, "Cy Edmunds" <cedmunds at spamless.rochester.rr.com> wrote:
>"hokiegal99" <hokiegal99 at hotmail.com> wrote in message
>news:93f5c5e9.0308021815.44c57a2d at posting.google.com...
>> How would I determine if a filename is greater than a certain number
>> of characters and then truncate it to that number? For example a file
>> named XXXXXXXXX.txt would become XXXXXX
>>
>> fname = files
>> if fname[0] > 6
>> print fname[0]
>>
>> Thanks!!!
>
>filename = "abcdefgh"
>if len(filename) > 6:
> filename = filename[:6]
>print filename
>abcdef
Why the if test?
>>> for filename in ['abcdefgh'[:i] for i in range(8)]:
... print 'filename=%r gets you filename[:6] => %r' % (filename, filename[:6])
...
filename='' gets you filename[:6] => ''
filename='a' gets you filename[:6] => 'a'
filename='ab' gets you filename[:6] => 'ab'
filename='abc' gets you filename[:6] => 'abc'
filename='abcd' gets you filename[:6] => 'abcd'
filename='abcde' gets you filename[:6] => 'abcde'
filename='abcdef' gets you filename[:6] => 'abcdef'
filename='abcdefg' gets you filename[:6] => 'abcdef'
I wonder about the application of this for filenames though, since filenames
often share prefixes.
Regards,
Bengt Richter
More information about the Python-list
mailing list