Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I know that many of the working directory prompts that work in IPython also work in Spyder as long as they're prefaced by %.

For example, pwd and ls work in IPython, but to run the same commands in Spyder they need to be prefaced with a % such as: %pwd and %ls.

To change the directory in IPython, I can run the cd command like: cd C:Users... HOWEVER, this doesn't seem to work in Spyder, even when prefaced with a %. Any suggestions?

I know that os.chdir ('C:\Users\') works, just trying to understand why %cd C:Users doesn't...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
148 views
Welcome To Ask or Share your Answers For Others

1 Answer

When you run %foo, that command is run in a new shell instance running as its own process. When that shell exits, changes to its state (such as its working directory) are lost with it; they don't effect the parent Python process that spawned it.

This is the same as how running sh -c 'cd /' doesn't change your current working directory in shell. (Indeed, running a new process as sh -c "$some_command" is exactly how the standard-C library call system(some_command), and its Python equivalent os.system(some_command) works).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...