1

I tried to use Python to run the following command lines. The first command line can always work, but the second one cannot and I don't know why.

import os, sys

os.system('IF EXIST C:\APC (echo 1) else (mkdir C:\APC)')

os.system('IF EXIST C:\APC\3d (echo 1) else (mkdir C:\APC\3d)')

If anyone knows the answer, pls let me know thanks!

ryan9025
  • 257
  • 5
  • 16
  • try with raw prefix `r'IF EXIST C:\123 (echo1) else (mkdir C:\123)`. But you really should do this using `os` module in full python... `\1` is `\x01` illegal path name – Jean-François Fabre Sep 29 '17 at 11:58
  • are those the real paths? – Jean-François Fabre Sep 29 '17 at 11:59
  • @Jean-FrançoisFabre I just tried raw prefix but it still doesn't work? – ryan9025 Sep 29 '17 at 12:01
  • are those the real paths or just an example? – Jean-François Fabre Sep 29 '17 at 12:02
  • If file paths contains space, quote them as so: `"C:\12 3\4 5 6\"` –  Sep 29 '17 at 12:03
  • 1
    Possible duplicate of [How can I create a directory if it does not exist?](https://stackoverflow.com/questions/273192/how-can-i-create-a-directory-if-it-does-not-exist) That was the first hit on searching with my favorite www search engine for `Python create directory`. Please first give a simple search a try before asking and writing such awful code. – Mofi Sep 29 '17 at 12:05
  • @Jean-FrançoisFabre It's just an example actually...pls see my edit with real paths – ryan9025 Sep 29 '17 at 12:07

1 Answers1

3

this works by sheer luck:

os.system('IF EXIST C:\APC (echo 1) else (mkdir C:\APC)')

because the backslashes don't escape anything (\A isn't an escape sequence)

But just paste the second command in a python REPL and see:

>>> 'IF EXIST C:\APC\3d (echo 1) else (mkdir C:\APC\3d)'
'IF EXIST C:\\APC\x03d (echo 1) else (mkdir C:\\APC\x03d)'

backslashed digits are interpreted as the actual byte value... Using raw string prefix fixes that:

>>> r'IF EXIST C:\APC\3d (echo 1) else (mkdir C:\APC\3d)'
'IF EXIST C:\\APC\\3d (echo 1) else (mkdir C:\\APC\\3d)'

but don't call system commands to test & create dirs. Use pure python to do that:

import os

d = r"C:\APC\3d"
if os.path.exists(d):
    print("exists")
else:
    os.mkdir(d)

It's more readable, easier to debug, you benefit of python exceptions and makes your code more portable on other platforms (well, not with that hardcoded path of course)

Jean-François Fabre
  • 126,787
  • 22
  • 103
  • 165
  • I still got a question. When I run this code the first time, it works well and the program can run the other remaining codes. However, when I run the second time, it will return "Cannot create a file when that file already exist", and prevent the remaining codes from running. I tried to replace "print" with "pass", but it still doesn't work? – ryan9025 Sep 29 '17 at 12:37
  • I'm puzzled, as `os.mkdir` is run only when `d` doesn't exist. Sure you're running this exact code? and the error originates from here? – Jean-François Fabre Sep 29 '17 at 12:45
  • Oh I didn't use `d` but the full directory. After I replace it with `d` then it works well! Why would this happen though? Thanks! – ryan9025 Sep 29 '17 at 13:11