writing command line output to file

前端 未结 3 778
粉色の甜心
粉色の甜心 2021-01-12 21:07

I am writing a script to clean up my desktop, moving files based on file type. The first step, it would seem, is to ls -1 /Users/user/Desktop (I\'m on Mac OSX).

3条回答
  •  失恋的感觉
    2021-01-12 21:19

    To open a file, you can use the f = open(/Path/To/File) command. The syntax is f = open('/Path/To/File', 'type') where 'type' is r for reading, w for writing, and a for appending. The commands to do this are f.read() and f.write('content_to_write'). To get the output from a command line command, you have to use popen and subprocess instead of os.system(). os.system() doesn't return a value. You can read more on popen here.

提交回复
热议问题