I am curious about the differences in leadings slashes in python. (Am on a windows machine- don’t know if that’s relevant.)
When would one use ../ vs ./ vs / vs no prefix for a path?
Would appreciate an explanation on the differences or any tips on where to find more information (e.g., search terms).
For example, if my python file is in dir1 and I want to save data.csv to dir2 I have found through trial and error that both ./dir2/data.csv and dir2/data.csv work for ‘csv_path’.
For example if this code is in a file in dir1,
with open(csv_path, 'w') as csvfile: headers = ['header1', 'header2'] writer = csv.DictWriter(csvfile, fieldnames = headers) writer.writeheader()
csv_path as …
../dir2/data.csv results in FileNotFoundError
/dir2/data.csv does not result in an error, but the file doesn’t appear to exist anywhere
And, as mentioned above ./dir2/data.csv and dir2/data.csv both create a file in the desired location, but I don’t know why or which one is more appropriate to use when.
user23468699 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.