error in os.chdir

Chris Angelico rosuav at gmail.com
Sat Jun 30 19:42:42 EDT 2018


On Sun, Jul 1, 2018 at 9:36 AM, eryk sun <eryksun at gmail.com> wrote:
> On Sat, Jun 30, 2018 at 11:21 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> On Sat, Jun 30, 2018 at 9:05 PM, Sharan Basappa
>> <sharan.basappa at gmail.com> wrote:
>>>
>>> 0
>>> down vote
>>> favorite
>>>
>>> I need to change directory to my local working directory in windows and then open a file for processing.
>>> Its just a 3 lines code, as below:
>>> import csv
>>> import os
>>> os.chdir('D:\Projects\Initiatives\machine learning\programs\assertion')
>>> The error is as follows:
>>> WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'D:\Projects\Initiatives\machine learning\programs\x07ssertion'
>>> Notice x07 character that has replaced character x07.
>>> I have a similar code but that goes through fine:
>>> import csv
>>> import os
>>> os.chdir('D:\Projects\Initiatives\machine learning\programs')
>>
>> Use forward slashes instead of backslashes for all paths.
>>
>> os.chdir('D:/Projects/Initiatives/machine learning/programs')
>
> Only use forward slashes for legacy DOS paths passed to Windows API
> functions. Do not use forward slashes for paths in command line
> arguments, \\?\ prefixed paths, or registry paths.

"Legacy" implies that it's the old standard that is now deprecated,
but the truth is that MOST path names are going to work fine with
forward slashes. Registry paths aren't file paths, command line
arguments are interpreted by the target program (and in many MANY
cases, forward slashes are absolutely fine), and so it's just the \\?\
paths that will need to be special. So, let me rephrase what you said:

Forward slashes work for all Windows file paths, except those that are
prefixed with \\?\.

Is that correct?

ChrisA



More information about the Python-list mailing list