Using variable in chdir function (module OS)

Asked

Viewed 302 times

0

It would be possible to use the informed data in a variable in some other function other than the chdir() module AS python?

I realized that it is not possible to use variables in the exchange of directories using this method. Would another of type? (can be from another module including).

The idea would be like this:

import os

def mudardir():
    a = 'Python'
    os.chdir('C:/Users/','a','diretoriob')

 mudardir()

As I do not know what is the username and variable of Windows itself %userprofile% is not accepted by the method, so I thought of setting earlier to later use within the function!

I am grateful for everyone’s attention!

  • "I realized that it is not possible to use variables in the exchange of directories using this method." <- Can you tell where you got this conclusion? In addition, your use of the method os.chdir does not seem to be correct. This function takes only one argument, but you are passing three.

  • You’re actually getting three arguments because I added the "a" variable in the middle there to explain the idea of how I’d like it to stay. Otherwise there would be only one ('C:/Users/Python/directory') that is correct...

  • In fact I was able to solve the problem. I was able to use the variable data by passing it as a parameter in the function!

  • So what is 'a'? It is the parent directory of the directory?

  • Right. Put your solution as an answer.

  • Exactly. But in my script in some cases I would not know the name of this parent directory, so before using the method os.chdir I would need to inform in a variable what would be the name of it... Then there arose the doubt of how I would add a variable in the argument, since the method accepts only one, and with the use of the variable would be more than one.

  • "Right. Put your solution as an answer." -> I’m a beginner here at SOF. How can I inform the solution? Just answer my own topic?

  • Just swap the commas for + and take the quotes from 'a'. Yes, you can answer your own question as if it were someone else’s.

Show 3 more comments

2 answers

1


To recover the user directory you can utilize the library os.path.expanduser, example:

from os.path import expanduser
user_dir = expanduser("~")
print(user_dir)

The exit in Windows will be similar:

'C:\\Users\\Thon'

Windows

The exit in Linux will be similar:

/home/wmsouza

You can see it working in repl it..

Or you can use the library pathlib.Path.home() available in Python 3.5+.

from pathlib import Path
user_dir = str(Path.home())
print(user_dir)

You can see it working in repl it..

Knowing this, just add a parameter in your method:

import os
from os.path import expanduser

def mudardir(diretorio):
    user_dir = expanduser("~")
    os.chdir(user_dir + diretorio)

# Em meu computador seria algo assim
# /home/wmsouza/Python
mudardir('/Python')
# /home/wmsouza/Python/Projeto_A
mudardir('/Python/Projeto_A')
# /home/wmsouza/Python/Projeto_B
mudardir('/Python/Projeto_B')

-1

I did + or - like this

do not need to do def, only a variable with the user and the directory

def cd(directory, user): os.chdir("c:/users/" + user + "/" + directory + "")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.