0

Can anyone tell how to skip forward slash char in python?

I want to create a directory abc(17/12/18) so I tried

import os
os.makedirs('abc(17\/12\/18)')

but the folder created was abc(17\)

Can anyone tell what am I missing? I searched on Internet but was unsuccessful.

vaultah
  • 44,105
  • 12
  • 114
  • 143
Akash Chandra
  • 375
  • 1
  • 4
  • 13
  • 1
    A Slash in a file or folder name is almost always a bad idea and with some os not even allowed (windows for example). A better seperator would be an underscore: abc(17_12_18) – Igl3 Jan 18 '18 at 14:06
  • Furthermore the function `os.mkdir` is what you would use to create a single directory. `os.makedirs` is the _Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory._ – Igl3 Jan 18 '18 at 14:11

3 Answers3

1

You don't need to escape forward slashes in python, only backslashes. The reason you cannot use that filename is that forward slashes are illegal in windows filenames. try this:

import os
os.makedirs('abc(17-12-18)')
Evan
  • 2,120
  • 1
  • 15
  • 20
1

You could do this.

import os

os.makedirs('abc(17' + u'\u2215' + '12' + u'\u2215' + '18)')

# This will create a directory named abc(17∕12∕18)
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
0

In Windows and Linux, / is not allowed in folder name.

More information here: https://stackoverflow.com/a/31976060/3813027

senerh
  • 1,315
  • 10
  • 20