Doubled backslashes in Windows paths [Resolved]

BartC bc at freeuk.com
Fri Oct 7 20:02:58 EDT 2016


On 07/10/2016 18:41, Oz-in-DFW wrote:
> On 10/7/2016 12:30 AM, Oz-in-DFW wrote:
>> I'm using Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC
>> v.1900 32 bit (Intel)] on Windows 7
>>
>> I'm trying to write some file processing that looks at file size,
>> extensions, and several other things and I'm having trouble getting a
>> reliably usable path to files.
> Thanks to all who have replied.  It looks like I have this part fixed.
>
> I had it fixed sooner than I realized because of cranial-anal-retention
> - the os.path.getsize() call was not using the variable into which I was
> placing the corrected path.  D'oh!
>
> The root problem was that the path did not have a trailing slash when
> concatenated with the filename.  This was handled properly by an
> os.path.join() call as recommended by Dennis Lee Bieber.

But the error message in your OP showed what looked like a correctly 
formed path (apart from the quotes). Without a / or \ between dirpath 
and filename, the error message would have been different (probably 
about not being able to find the file).

> So what now works is
>
>             path = os.path.join(dirpath,name)
>             if os.path.getsize(path)>10000:
>                 print("Path: ",path," Size: ",os.path.getsize(path))
>
> Raw strings didn't apply because the content of /dirpath /and /name /are
> the result of an os.walk() call.

When I was creating such path-handling routines, anything returning the 
name of a path /always/ ended with \ or / for that reason: so that you 
could just stick a file at the end without needing to check whether the 
path had the right ending or not.

-- 
Bartc



More information about the Python-list mailing list