1

So what I'm trying to do is to join something in the form of

os.path.join('C:\path\to\folder', 'filename'). 

**edit : Actual code is :

filename = 'creepy_%s.pcl' % identifier
file = open(os.path.join(self.cache_dir, filename), 'w')

where self.cache_dir is read from a file using configobj (returns string) and in the particular case is '\Documents and Settings\Administrator\creepy\cache'

The first part is returned from a configuration file, using configobj. The second is a concatenation of 2 strings like: 'file%s' % name

When I run the application through the console in windows using the python interpreter installed, I get the expected result which is

C:\\path\\to\\folder\\filename 

When I bundle the same application and the python interpreter (same version, 2.6) in an executable in windows and run the app the result is instead

C:\\path\\to\\folderfilename

Any clues as to what might be the problem, or what would cause such inconsistencies in the output ?

ilektrojohn
  • 19
  • 1
  • 4
  • Cut-and-paste the exact line of code causing the issue. The code line you have could not have generated the listed output. The output from your example is `'C:\\path\to\x0colder\\filename'`, which actually has the correct joining backslash before filename. – Mark Tolonen Feb 13 '11 at 03:31
  • Ok, I just wanted to show that the result is the correct one. Probably because I was lucky not to have any folder or filename starting with f or t e.t.c. See above for the original code – ilektrojohn Feb 13 '11 at 03:57

4 Answers4

0

Your code is malformed. You need to double those backslashes or use a raw string.

os.path.join('C:\\path\\to\\folder', 'filename'). 

I don't know why it works in one interpreter and not the other but your code will not be interpreted properly as is. The weird thing is i'd have expected a different output, ie: C:pathtofolder\filename.

SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • The thing is that its the same interpreter. In one case it is accessed normally, in the other it is bundled as a dll within the application. – ilektrojohn Feb 13 '11 at 03:25
  • "The weird thing is i'd have expected a different output" : Yeap , thats the strange part. But still , adding a second backslash makes it work correctly. – ilektrojohn Feb 13 '11 at 03:43
  • well the thing is `\t` is a tab character so maybe the difference is simply that a different part of the DLL or shell chokes on it to the interpreter version. Perhaps the issue is your config handler sanitising the input as distinct from you typing it by hand. – SpliFF Feb 13 '11 at 04:01
  • I don't thing so , configobj spits out strings . In any case I'm not typing anything by hand, I just run the application with `python myapp.py` instead of running myapp.exe . It's the same exactly code, with the same config files. – ilektrojohn Feb 13 '11 at 04:05
  • Maybe the wrong DLL is being linked then. Perhaps it's being loaded from a system path? have you tried outputting the python version in the compiled exe? – SpliFF Feb 13 '11 at 04:20
0

It is surprising behavior. There is no reason it should behave in such a way.

Just be be cautious, you can change the line to the following.

os.path.join(r'C:\path\to\folder\', 'filename'). 

Note the r'' raw string and the final \

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • You don't need the trailing backslash when working with `os.path.join` — that's what it's for. :-) – Ben Blank Feb 13 '11 at 03:28
  • @Ben, I read the ntpath.py code and figured out that it checks for path[-1] char and if it is '\', it appends the next arg to the path. This is what the poster is looking for. – Senthil Kumaran Feb 13 '11 at 03:34
  • — The -1 check is only to prevent `r"foo\"` and `r"\bar"` from joining as `r"foo\\bar"`. `os.path.join("foo", "bar")` will return `r"foo\bar"`. – Ben Blank Feb 13 '11 at 06:56
0

Three things you can do:

  1. Use double-slashes in your original string, 'C:\\path\\to\\folder'

  2. Use a raw string, r'C:\path\to\folder'

  3. Use forward-slashes, 'C:/path/to/folder'

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • Hey thanks for the suggestions.Forward slashes and double slashes worked, but still I get the first part of the dir from a config file , or from os.environ . So, a little more work to add a second backslash I guess. – ilektrojohn Feb 13 '11 at 03:41
0

I figure it out yesterday. As usual when things seem really strange, the explanation is very simple and most of the times involve you being stupid.

To cut a long story short there were leftovers from some previous installations in dist-packages. The bundled interpreter loaded the module from there , but when i ran the python script from the terminal , the module (newer version) in the current dir was loaded. Hence the "unpredictable" results.

ilektrojohn
  • 19
  • 1
  • 4