classification
Title: os.remove() auto add \ in Windows2012R2
Type: resource usage Stage: resolved
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: 007hengxyx, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-08-02 07:25 by 007hengxyx, last changed 2017-08-02 07:42 by paul.moore. This issue is now closed.

Messages (2)
msg299645 - (view) Author: Re-ax (007hengxyx) Date: 2017-08-02 07:25
I need to remove a file in python at Windows2012R2, but, os.remove()auto add \ in each seq.

code:
#coding=utf-8
import os
dir_path='d:\c\d\e\t\c\t.xf'
os.remove(dir_path)


result:
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 

Traceback (most recent call last):
  File "D:\test\script\test.py", line 4, in <module>
    os.remove(dir_path)
WindowsError: [Error 3] The system cannot find the path specified: 'd:\\c\\d\\e\t\\c\t.xf'
>>>
msg299646 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2017-08-02 07:42
There are two problems with your code and bug report:

1. By using a single quoted string, some of the backslashes in the path are being interpreted as starting a special character (specifically \t is interpreted as a tab character). You should either double the backslashes to prevent this interpretation (dir_path='d:\\c\\d\\e\\t\\c\\t.xf'), use forward slashes (dir_path='d:/c/d/e/t/c/t.xf') or use a raw string (dir_path=r'd:\c\d\e\t\c\t.xf').

2. You're reporting that Python "auto adds \". It doesn't, it's just that the repr of the string shows a single quoted string with backslashes doubled - that's the standard repr for strings.

So, Python is behaving as expected, and there's no bug here.
History
Date User Action Args
2017-08-02 07:42:15paul.mooresetstatus: open -> closed
resolution: not a bug
messages: + msg299646

stage: resolved
2017-08-02 07:25:45007hengxyxcreate