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
  • 40,483
  • 12
  • 109
  • 137
Akash Chandra
  • 315
  • 4
  • 11
  • 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) – Igle 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._ – Igle 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
  • 1,830
  • 1
  • 14
  • 18
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
  • 12,736
  • 2
  • 17
  • 36
0

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

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

senerh
  • 1,265
  • 10
  • 19