How to use the dir/s command in Python?

后端 未结 5 1973
[愿得一人]
[愿得一人] 2021-01-13 07:13

Background

I use the command dir/s in batch files all the time. But, I am unable to call this using python. NOTE: I am

5条回答
  •  太阳男子
    2021-01-13 08:09

    You need a space between dir and /s. So break it into an array of 2 elements. Also as carlosdoc pointed out, you would need to add shell=True, since the dir command is a shell builtin.

    import subprocess
    subprocess.call(["dir", "/s"], shell=True)
    

    But if you're trying to get a directory listing, make it OS independent by using the functions available in the os module such as os.listdir(), os.chdir()

提交回复
热议问题