Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Invalid argument in Python

I am trying to create some file paths to create new csv file in the target folder.
My code is as follows.

#The original path.
path_1=r'C:\Users\hh\OneDrive - Technology, Inc\IE'

#Creating the new path with the original path.
folder_name='New Folder'
x='\\'
path_2='r'+"'"+path_1+x+folder_name+"'"

#Creating the other new path with csv file name.
import csv
csv_name='r'+"'"+path_1+x+folder_name+x+'New File.csv'+"'"

#Creating a new csv file in the target folder.
with open(csv_name, 'wb') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL) 

I had checked the paths I created many times, and didn’t find anything wrong. However, It keeps showing the error as follows.

[Errno 22] Invalid argument: "r'C:\\Users\\hh\\OneDrive - Technology, Inc\\IE\\New Folder\\New File.csv'"

Please help me find out what did I do wrong.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Remove the ‘r’ +
And then, if you want concatenate file path, use os.path.join() like this

import os


folder_name = r"C:/test/today"
name = "test.csv"
print(os.path.join(folder_name, name))  # C:/test/today/test.csv
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading