Save output of os.system to text file

后端 未结 4 449
悲&欢浪女
悲&欢浪女 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:05

    Try the following:

    import os 
    import subprocess 
    import time 
    
    print ('Current Directory: {}'.format(os.getcwd()) ) 
    print ('Opening Android SDK...') 
    os.chdir('C:\\android-sdk\\platform-tools') 
    print ('Current Directory: {}'.format(os.getcwd()) )
    t = str(time.ctime()) 
    try:
        process_output = subprocess.check_output(["adb", "devices", "-l"])
    except: #Add here the type of exception you want to raise and logic
        print("Please check your ADB installation and syntax.")
    s = ('{} Checking for connected devices: {}'.format(t,process_output) ) 
    with open('logfile.txt', 'w') as f:
        f.write(s) 
    

提交回复
热议问题