0

When i use os.getcwd() , it gives me something like this :

'/home/xxx/PycharmProjects/pythonProject3'

I want change it to something like this :

PycharmProjects/pythonProject3

in another word I wanna my os.getcwd() starts from where I want. how can I do it? i tried os.chdir and os.path.abspath(os.curdir) and it does not work. I am using python3 and ubuntu.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
ex3p
  • 13
  • 2
  • After you clarified in the comments below the answer I think this is a duplicate. There one case for doing it programmatically [*"Python: Get relative path from comparing two absolute paths"*](https://stackoverflow.com/questions/7287996) and another case for configuring the cwd in the IDE settings [*"PyCharm current working directory"*](https://stackoverflow.com/questions/34304044). – bad_coder Mar 22 '21 at 14:03

1 Answers1

0

Can you tell us how did you try chdir? Please see example below to change you current working director and let me know that output it gives on your machine.

>>> import os
>>> os.getcwd()
'/home/lllrnr101'
>>> os.chdir('/etc/')
>>> os.getcwd()
'/etc'
>>> 
lllrnr101
  • 2,288
  • 2
  • 4
  • 15
  • I think I express my purpose wrong. with executing what you said I got answer same as you but I wanna change my starting place to somewhere what I want for example --> ``` /home/xxx/PycharmProjects/pythonProject3``` to ```PycharmProjects/pythonProject3``` in another word getcwd shows me ```PycharmProjects/pythonProject3``` instead of showing ``` /home/xxx/PycharmProjects/pythonProject3``` – ex3p Mar 22 '21 at 13:42
  • cwd means current working directory. This is always an absolute path, not a relative one. – stark Mar 22 '21 at 13:56
  • You're right, so how can I do what I want? can you give a command or a hint? – ex3p Mar 22 '21 at 13:59
  • @ex3p There is no need to change cwd to relative, because you can do relative paths anyway. Edit your question to explain what you really want to do. – stark Mar 22 '21 at 16:00