Save output of os.system to text file

后端 未结 4 430
悲&欢浪女
悲&欢浪女 2020-12-04 00:42

I\'m not great on all the technical terms so I\'ll do my best to explain my problem.

I\'ve written a small script to open android SDK and check for attached devices

4条回答
  •  独厮守ぢ
    2020-12-04 01:26

    Thanks everyone for your help. The answer was:

    import os
    import time
    import subprocess
    
    print 'Current Directory:', os.getcwd()
    print 'Opening Android SDK...'
    os.chdir('C:\\android-sdk\\platform-tools')
    print 'Current Directory:', os.getcwd()
    print 'Checking for connected devices:'
    t = time.ctime()
    # save log of time and connected devices
    with open('logfile.txt', 'w') as f:
        s = ('{}\n{}'.format(t, subprocess.check_output(["adb", "devices", "-l"])))
        f.write(s)
    print(s)
    

提交回复
热议问题