1

I would like to write the file path with an windows environment variable for the windows username but I don't know how to write it correctly. I would like to use an env variable because I use the script on different computers.

Here is the code:

import os

os_username = os.environ.get('USERNAME')

workbook = xlsxwriter.Workbook(r"C:\Users\" + os_username + r"\Google-Drive\filename.xlsx")

The above code does not work to find the correct path. The code for finding the username is working. The file path would be for instance: C:\Users\Peter\Google-Drive\filename.xlsx

1 Answer 1

2

You should create string for your path. And you can use '/' or 'double backslash' instead

Try this:

my_path = fr"C:/Users/{os_username}/Google-Drive/filename.xlsx"
workbook = xlsxwriter.Workbook(my_path)

see here for windows path

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.