Doubled backslashes in Windows paths [Resolved]

Oz-in-DFW lists at ozindfw.net
Fri Oct 7 13:41:51 EDT 2016


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.

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.

As Dennis Lee Bieber also observed, the call to

               path = os.path.normpath(path)

wasn't needed. It does no harm as it changes the slash directions to
keep the Windows command line happy, but the Python functions are just
as happy without the changes.

Thanks to all who replied.

-- 
mailto:oz at ozindfw.net    
Oz
POB 93167 
Southlake, TX 76092 (Near DFW Airport) 






More information about the Python-list mailing list