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)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • [What is the correct cross-platform way to get the home directory in Python?](https://stackoverflow.com/a/4028943/1518100) then `os.path.join` – Lei Yang Mar 09 '22 at 01:49
  • Your first try would work if you *called* `os.getlogin()` (add parentheses). The second works if `val` is defined correctly. The last two are just wrong mixing f-strings and `.format`. – Mark Tolonen Mar 09 '22 at 02:02

3 Answers3

0

"r" means the string will be treated as raw string so try removing that and using escaped characters target = "C:\\Users\\{val}\\CodeProjects\\WebSafer".format(val = os.getlogin)

buithienquyet
  • 351
  • 1
  • 6
  • then this is the error I get: [Errno 2] No such file or directory: 'C:\\Users\\User\\CodeProjects\\WebSafer' –  Mar 09 '22 at 01:53
  • you should check more https://appdividend.com/2021/07/03/how-to-create-directory-if-not-exist-in-python/ if your destination path is not exist – buithienquyet Mar 09 '22 at 01:56
0

Don't mix f with .format, this is working for me:

import os

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

And I think better way is:

import os.path
from pathlib import Path

home = str(Path.home())
print(os.path.join(home, "CodeProjects\WebSafer"))

Then if you encounter some error when copying, you need clarify what you want to copy, copy a file, or a folder, if a folder, should it go to within the dest folder, or overwrite dest folder?
You may want try different methods such as shutil.copy, shutil.copytree, and different parameters.

Lei Yang
  • 3,970
  • 6
  • 38
  • 59
  • if you are using pathlib, `os.path.join()` isn't necessary, you can use: `home = Path.home(); target_path = home / "CodeProjects" / "WebSafer"` – monkut Mar 09 '22 at 02:05
  • i want avoid hardcode the path seprator. isn't `os.path.join()` more cross platform? – Lei Yang Mar 09 '22 at 02:12
  • pathlib gives you the same 'cross platform' ability as `os.path.join()` . However, in the current example the "CodeProject\WebSafer" contains a slash and would not be cross platform. – monkut Mar 09 '22 at 03:24
0

You can use f-string (to directly enter your variable) and r-string (to enter the path without escape characters like \) together.

val = os.getlogin() # Returns username
target = fr"C:\Users\{val}\CodeProjects\WebSafer"

If you're getting a No such file or directory error, it means that the actual file or folder does not exist. Check the actual path to ensure every part (Your Username, CodeProjects, Websafer) exists on your computer.

In case you don't know if your user will have that folder on their system, you can use a try-except block to alert the user or to revert to some default folder instead.

codingray
  • 106
  • 7