title: Converting windows paths to pathlib.WindowsPath() within a function in python
body: <p><em><strong>Edited for clarity</strong></em></p>
  <p>I need to be able to copy and paste a windows path <strong>directly</strong> from file explorer into a function which turns it into a <code>pathlib.WindowsPath()</code> object.</p>
  <p>For example: what I want is something like.</p>
  <pre><code>def my_function(my_directory):
      my_directory = pathlib.WindowsPath(my_directory) #this is the important bit 
      files = [e for e in my_directory.itirdir()]
      return(files)
  
  new_list = my_function('C:\Users\user.name\new_project\data')
  
  print(new_list[0])
  
  OUT[1] 'C:\\Users\\user.name\\new_project\\data\\data_set_1'
  
  </code></pre>
  <p>When I try this I get the error:</p>
  <pre><code>SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape (&lt;string&gt;, line 1)
  </code></pre>
  <p>Now I know that this is because of the <code>\n</code> in my string my windows path that I'm passing to the function, and that this will be fixed if i pass it in as <code>r'C:\Users\user.name\new_project\data'</code> but this isn't a practical solution to my issue. Is there a way I can fix this by converting the windows path to a raw string <strong>within my function</strong>?</p>
  <p>I have tried:</p>
  <pre><code>def my_function(my_directory):
      input = fr'{my_directory}'# converting string to raw string
      path = pathlib.WindowsPath(input)
      files = [e for e in path.itirdir()]
      return(files)
  
  new_list = my_function('C:\Users\user.name\new_project\data')
  
  print(new_list[0]
  
  OUT[1] 'C:\\Users\\user.name\\new_project\\data\\data_set_1'
  
  </code></pre>
  <p>but no joy.</p>
  <p>Any help would be appreciated.</p>