title: How to account for special characters in windows file paths - python
body: <p>I am trying to create a script that goes through windows files and removes "." form the file name with the path entered as a string. The problem starts when any folder in the path starts with a  <code>a</code>, <code>b</code>, <code>e</code>, <code>f</code>, <code>n</code>, <code>r</code>, <code>s</code>, <code>t</code>, <code>v</code> or <code>x</code> (Backslash Notation in python). Then the system throws a fit saying this is not a valid path (specifically with the error message below). A workaround is to use <code>"/"</code> in the path and run <code>os.path.normpath(folder)</code> But I cant seem to find a way to pick up all the <code>\</code> to switch them at runtime (python ignores ones with the above list of characters after them so round and round we go).</p>
  
  <p>Error message:</p>
  
  <pre><code>Traceback (most recent call last):
  File "C:\Users\Mendel\OneDrive\Code\Python - 2\fileNameCleanUp.py", line 39, in &lt;module&gt;
  rename(test_folder)
  File "C:\Users\Mendel\OneDrive\Code\Python - 2\fileNameCleanUp.py", line 21, in rename
  for filename in os.listdir(folder):             #for every file in the folder
  WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\Mendel\\Desktop\rmdot\\*.*'
  </code></pre>
  
  <p><strong>EDIT 1</strong><br>
  Code:</p>
  
  <pre><code>import sys, os
  
  
  def rename(folder):
      //folder = os.path.normpath(folder)
      print "Folder: = ", folder
      if not folder.endswith('\\'):       #Check for windows path divider at the end
          if folder.find('\\') &gt; 0:       #Check if it is windows
              folder += '\\'              #Add path divider to the end
  
      print "Folder: = ", folder
      #Rename files
      for filename in os.listdir(folder):             #for every file in the folder
          infilepath = os.path.join(folder, filename) #make a path out of it
          if os.path.isfile(infilepath) == False:     #Check if it is valid
              continue                                #if not: next file
  
          nameParts = filename.split(".")
          print "\nNew File:"
          print "nameParts: ", nameParts
          print "Length: ", len(nameParts)
          print "Length - 1: ", nameParts[len(nameParts) - 1]
  
  
  test_folder = "C:/Users/Mendel/Desktop/rmdot"
  rename(test_folder_2)
  </code></pre>
  
  <p><strong>TL;DR</strong>: How do I account for backslash notation in windows paths entered as strings using python?</p>
  
  <p>Thanks,</p>