Separate the result that appears in os.getcwd() to be able to display only c:

Asked

Viewed 152 times

0

Make a program that displays the current directory (which you are saving your programs to), the disk drive used, the system user name and the current folder. Consider the example below. Tip use the function split.

Diretorio Atual = C:\Users\monte\PycharmProjects\Aula3
Unidade do Disco = C:
Nome do Usuario = monte
Pasta Atual = Aula3

I tried to use the split and the program did not accept

import os
os.getcwd()
os.getlogin()
lista = []
print(os.getcwd())
print(os.getlogin())
lista.append(os.getcwd()) #variável colocada na lista
  • At what point did you try to use the split? Why the program didn’t accept, it was wrong?

  • I tried using after the "list.append(os.getcwd()). error appeared:

  • Traceback (Most recent call last): File "C: Users cwo Downloads list.py", line 82, in <module> list.split() Attributeerror: 'list' Object has no attribute 'split'

  • The function split serves to break a string in pieces, you used it in a list.

  • appeared this result to me: C: Users cwo Downloads so I tried to use the function to separate c: from what appears

  • I think that’s the answer. import os os.getcwd() os.getlogin() list = [] print('Current Directory', os.getcwd()) print('User: ', os.getlogin() list.append(os.getcwd()) list = '. Join(list) list.split() print('Disk Drive: ', list[0:2]) print('Current Folder: ', list[13:22])

Show 1 more comment

3 answers

1


Use the functions of os.path!

Assuming you have a variable d with the directory you want (you can use os.getcwd() to get the current directory, but in the example below I will use the same directory that you exemplified, to illustrate):

>>> # d = os.getcwd()
>>> d = r'C:\Users\monte\PycharmProjects\Aula3'

To get the drive, use os.path.splitdrive

>>> drive, resto = os.path.splitdrive(d)
>>> print(drive)
C:

To get the current folder, use os.path.basename:

>>> pasta = os.path.basename(d)
>>> print(pasta)
Aula3

See more on documentation of os.path here.

  • Thank you very much, nosklo. my program looks like this: import os os.getcwd() os.getlogin() list = [] print('Current Directory:', os.getcwd()) print('User:', os.getlogin()) list.append(os.getcwd()) list = '. Join(list) list.split() print('Disk Drive:', list[0:2]) #print('Current Folder:', list[13:22])

  • However I used basename and I did not have the return of the current directory.

  • I managed to build the program. Thanks for the help!!! &#xA;&#xA;import os&#xA;os.getcwd()&#xA;os.getlogin()&#xA;d = os.getcwd()&#xA;lista = []&#xA;print('Diretorio Atual:', os.getcwd())&#xA;print('Usuario:', os.getlogin())&#xA;lista.append(os.getcwd())&#xA;pasta_atual = os.path.basename(d)&#xA;lista = ''. Join(list) list.split() print('Disk Drive:', list[0:2]) print('Current Folder:',current)

0

Using the function split as stated above, I suggest the following solution:

Consider the following directory as an example:

d = r"C:\Users\monte\PycharmProjects\Aula3"
# OU 
d = "C:\\Users\\monte\\PycharmProjects\\Aula3"

Note that in the second case was placed against double bars \\. This is necessary for Python to recognize the backslash \ as a character.

The function split comes in next:

d_list = d.split("\\")
print(d_list)
['C:', 'Users', 'monte', 'PycharmProjects', 'Aula3']

Note that this way we already have all the desired fields separated. Now just take the fields of interest:

Unidade_do_Disco = d_list[0]
Nome_do_Usuario = d_list[2]
Pasta_Atual = d_list[-1]

One last remark: when calling the function split without any parameter, the default is to separate the string by whitespace. When passing the parameter, the string is separated using the parameter passed as the criterion.

0

Make a program that displays the current directory (which you are saving your programs to), the disk drive used, the system user name and the current folder. Consider the example below. Tip use split function.

Current Directory = C: Users mount Pycharmprojects Aula3

Disk Drive = C:

User name = mount

Current Folder = Class3

import os
os.getcwd()
os.getlogin()
d = os.getcwd()
lista = []
print('Diretorio Atual:', os.getcwd())
print('Usuario:', os.getlogin())
lista.append(os.getcwd())
pasta_atual = os.path.basename(d)
lista = ''.join(lista)
lista.split()
print('Unidade de Disco:',lista[0:2])
print('Pasta Atual: ',pasta_atual)

Browser other questions tagged

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