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
  • 101
  • 2
  • `:` is not a character that is allowed in file or folder names on Windows. – Mokubai Jun 21 '18 at 15:42
  • @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 Jun 21 '18 at 15:57
  • Sorry, got sidetracked. Yes, you would need to handle whatever substitutions you are doing before trying to make the directories. – Mokubai Jun 21 '18 at 16:34

0 Answers0