writing command line output to file

前端 未结 3 801
粉色の甜心
粉色の甜心 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:45

    You can redirect standard output to any file using > in command.

    $ ls /Users/user/Desktop > out.txt
    

    Using python,

    os.system('ls /Users/user/Desktop > out.txt')
    

    However, if you are using python then instead of using ls command you can use os.listdir to list all the files in the directory.

    path = '/Users/user/Desktop'
    files = os.listdir(path)
    print files
    

提交回复
热议问题