Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23390

How do I put a variable in a path

$
0
0

I'm trying to store a path in a variable. see below

target = r"C:\Users\User\CodeProjects\WebSafer"

However, I need it to be dynamic. Not hardcoded to my username, so I get the login username by doing:

val = os.getlogin()

So I need to put the variable val in the path. But every time I tried doing it I always get a truncating/syntax error. Please help me! Below is the code snippet:

print("No copy found...making a copy\n")val = os.getlogin()original = r"C:\*******\********\*******\***\****"target = r"C:\Users\User\CodeProjects\WebSafer"shutil.copy(original, target)

The "*" are just for privacy reasons, there actually replaced with the right path location to what I'm copying.

What I have tried so far:

target = r"C:\Users\{val}\CodeProjects\WebSafer".format(val = os.getlogin)target = r"C:\Users\{}\CodeProjects\WebSafer".format(val)target = rf"C:\Users\{val}\CodeProjects\WebSafer".format(val = os.getlogin)target = rf"C:\Users\{}\CodeProjects\WebSafer".format(val)

Viewing all articles
Browse latest Browse all 23390

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>