Oscar Diego
Oscar Diego

Reputation: 43

Percent signs in windows path

I want to do this path ('C:\\Users\\%username%\\AppData') but the percent signs used to determine who the user is, is messing up the whole path. I know that you use double backslashes but what do I do when it comes to percent signs?

Thank you, sincerely a python noob :)

Upvotes: 4

Views: 5446

Answers (2)

Jonah Bishop
Jonah Bishop

Reputation: 12591

According to the documentation, I think you want to use the os.path.expandvars routine.

On Windows, %name% expansions are supported in addition to $name and ${name}.

import os
my_path = "C:\\Users\\%username%\\AppData"
expanded_path = os.path.expandvars(my_path)
print("The expanded path is: {}".format(expanded_path))

This example works for me in the Python 3.6 command prompt in Windows 7.

Upvotes: 3

noname1014
noname1014

Reputation: 101

You may want to try using os.system:

e.g.

import os
os.system('PATH')

Upvotes: 0

Related Questions