Post #353,835
1/31/12 2:48:18 PM
|
Well that was fun. A NULL in a directory name?
I'm writing a little python script on Winders to massage some data.
One of my data directories is named "00-LatestData" to make it easy to find in a search, to have it at the top of a listing, etc.
I want to change the CWD to a subdirectory in that tree.
>>> import os
>>> myDataDir = "c:\data\00-LatestData"
>>> os.chdir(myDataDir)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string without null bytes, not str
>>> myDataDir = "c:\data/00-LatestData"
>>> os.chdir(myDataDir)
>>> print os.getcwd()
c:\data\00-LatestData
>>>
I now see that "\00" is a NULL, so some trickery is required.
A little annoying, but relatively easy to fix.... :-/
Cheers,
Scott.
(Who has sympathy for you guys who have to deal with details like this all the time...)
|
Post #353,836
1/31/12 3:17:01 PM
|
There is also a difference...
in passing arguments with Windows 7 vs "Vista and before"
We had a customer with "download directory" that had a space in the path. Our software put CSV and Excel Files into it.
Our software would launch Excel and pass along the file name, properly quoted and/or escaped. Previous to Win7 and Excel 2010, this worked with out a problem. WinXP and Excel2010 no problem.
Effectively even though it was properly escape, Excel would complain that it was trying to open a non-existent directory, a non-existent file and a non-existent file. Three errors.
Trickery indeed.
|
Post #353,837
1/31/12 3:48:32 PM
|
Re: Well that was fun. A NULL in a directory name?
I know nothing about Python, but have you tried:
>>> myDataDir = "c:\\data\\00-LatestData"
Hell, I don't even know if that's legal.
-Mike
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety."
- Benjamin Franklin, 1759 Historical Review of Pennsylvania
|
Post #353,843
1/31/12 7:28:49 PM
1/31/12 7:30:19 PM
|
What Mike said...
Actually, \0 is the null. Mike's suggestion is correct, but also take a look at the os.path module to avoid the pain.
FWIW, the board doesn't like it either :-/ I had to escape the backslash or it shows as "0".
Edited by scoenye
Jan. 31, 2012, 07:30:19 PM EST
|
Post #353,846
1/31/12 7:54:29 PM
|
Thanks for the correction.
There's an awful lot of stuff in the python standard library! :-)
I too found that you can do "\\" and "\0" here - you just have to double the number of backslashes you want to be visible.
Apparently this "null in a string" issue is something that python web programmers have to think about - http://lucumr.pocoo....as-web-developer/
Thanks.
Cheers,
Scott.
|
Post #353,850
1/31/12 8:16:35 PM
|
Backslashes are for escaping things
Including backslashes. Working as designed.
Regards, -scott Welcome to Rivendell, Mr. Anderson.
|
Post #353,844
1/31/12 7:44:51 PM
|
It's an easy fix.
There's likely a way to escape the backslash, but I just did a substitution.
testing inside the Python shell (omitting earlier steps):
>>>myDataDir = myDataDir.replace("\\", "/") # escape the backslash in the command
>>>os.chdir(myDataDir)
Easy, peasy.
Your solution works, too:
>>> myDataDir = "c:\\data\\00-LatestData"
>>> os.chdir(myDataDir)
>>> print os.getcwd()
c:\data\00-LatestData
>>>
There may even a 'os' method to give the 'myDataDir' already pre-escaped; no doubt this has come up before... But I learned some things with my adventures today, so it's all good.
Thanks.
Cheers,
Scott.
|
Post #353,838
1/31/12 3:51:17 PM
|
for what it's worth
We run Windows 7 here at work, so I opened Windows Explorer to create the folder "Data" on the C: drive. Inside that I created "00-LatestData", and inside that I created "AnotherScott".
Went to the Start button, opened a DOS prompt, and entered "cd" to get to the root of C: drive.
Entered "cd data" (without the quotation marks)
Entered "00-LatestData" (without the quotation marks)
Entered "AnotherScott" (without the quotation marks)
Accessing it via DOS worked okay.
"Chicago to my mind was the only place to be. ... I above all liked the city because it was filled with people all a-bustle, and the clatter of hooves and carriages, and with delivery wagons and drays and peddlers and the boom and clank of freight trains. And when those black clouds came sailing in from the west, pouring thunderstorms upon us so that you couldn't hear the cries or curses of humankind, I liked that best of all. Chicago could stand up to the worst God had to offer. I understood why it was built--a place for trade, of course, with railroads and ships and so on, but mostly to give all of us a magnitude of defiance that is not provided by one house on the plains. And the plains is where those storms come from."
-- E.L. Doctorow
|
Post #353,845
1/31/12 7:47:38 PM
|
It's not an OS issue, it a python method issue.
The command to change the current working directory doesn't like seeing "\0" in the parameter string. So, you have to do some manipulation, or use the proper routines that take care of it for you.
See above for some solutions.
Thanks.
Cheers,
Scott.
|
Post #353,847
1/31/12 8:03:35 PM
|
Not just Python
A C version would lead to some headscratching as you wonder why cwd puts you a level above what you intended.
|
Post #353,852
1/31/12 8:28:55 PM
|
Null terminated strings are evil.
Yeah, it's something that made sense at the time when C was designed, but now...
</DonQuixote>
Cheers,
Scott.
|
Post #353,848
1/31/12 8:13:53 PM
|
This is, of course, actually a Windows bug.
Microsoft should never have used a backslash as a path separator. But they did because they wanted to use the forward slash for options (like in VMS).
Wade.
|
Post #353,851
1/31/12 8:22:48 PM
|
Blame Kildall.
http://xdal.org/~benoit/rants/backslash
:-)
Cheers,
Scott.
(Who sees from the internal link that maybe we should blame IBM and ALGOL, too.)
|