Open File in Another Directory (Python)

If you know the full path to the file you can just do something similar to this. However if you question directly relates to relative paths, that I am unfamiliar with and would have to research and test. path=”C:\\Users\\Username\\Path\\To\\File” with open(path, ‘w’) as f: f.write(data) Edit: Here is a way to do it relatively instead … Read more

Python os.path.join() on a list

The problem is, os.path.join doesn’t take a list as argument, it has to be separate arguments. To unpack the list into separate arguments required by join (and for the record: list was obtained from a string using split), use * – or the ‘splat’ operator, thus: >>> s = “c:/,home,foo,bar,some.txt”.split(“,”) >>> os.path.join(*s) ‘c:/home\\foo\\bar\\some.txt’