Windows 10 mkdir in Python Script with Special Character

0

I think/hope this a an easy question that I just can't find the answer to - how do I create a directory where the title contains a colon? I'm scraping data from a website with variables for section and year, and creates a directory:

if 'C:Path/Data/%s' % stat:
                directory = 'C:Path/Data/%s' % stat                
            else:                
                directory = 'C:Path/Data/%s' % stat.replace('/', ' ')
            if not os.path.exists(directory):
                try:
                    os.makedirs(directory)
                except OSError as e:
                    if not e.errno != errno.EEXIST:
                        raise

I have some error handling in there based on what's coming back. I had to bring in errno to skip the section that was erring. I want to bring in that data however - and I believe the issue is the title of the section, and thus the directory, contains a : in a directory like:

/Year: some_data/

I receive this error:

OSError: [Errno 22] Invalid argument:
Failed with OSError

My question is - how do I create or handle the creation of a directory with a : in the title? I'm just skipping over it for now.

I apologize if this is a duplicate but I couldn't find anything specifically. I'm running python 3.6 in Jupyter Notebook on Windows 10. Thank you.

Hatt

Posted 2018-06-21T15:32:17.240

Reputation: 101

Question was closed 2018-06-21T15:41:49.183

: is not a character that is allowed in file or folder names on Windows. – Mokubai – 2018-06-21T15:42:33.030

@Mokubai Gotcha thanks - so I can't handle that error in this case. It needs to be tended to as I pull it down and before creating the directory? – Hatt – 2018-06-21T15:57:34.780

Sorry, got sidetracked. Yes, you would need to handle whatever substitutions you are doing before trying to make the directories. – Mokubai – 2018-06-21T16:34:02.810

No answers