2017-07-16 1 views
0

私は初心者のプログラマーです。私の目標は、smsコマンドを受け入れて、自分のメインPC上の対応する機能をリモートで実行できるユーティリティを設計することです。私はこれを行うためにさまざまなツールを使用しています(IFTTT、Dropbox、Twilio、Task Scheduler)が、私はちょっとしたトラブルに遭遇しました。あらかじめ定義されたpython関数がname = mainの場合に実行に失敗しました

最近、メインコードを 'main()'という名前の関数で実行しようとしました。しかし、私は関数を呼び出すしようとすると、コマンドラインがエラーをスロー:

Traceback (most recent call last): 
File "C:\Python_Files\WIP\Check.py", line 104, in <module> 
    main() 
NameError: name 'main' is not defined 

私は、コンテキストのためにここに私のコードを掲載します(いくつかの情報は、セキュリティのために編集された):

#DESCRIPTION: 
#Check if C:/Users/Thermaltake/Dropbox/Remote_Control contains a .txt file 
#set count to corresponding integer 
#delete file 
#call function of corresponding integer 
#reset count 
#Set Task Scheduler to run this program every 5 minutes when idle. 

import os 
import time 
import subprocess 
import sys 
import collections 
import webbrowser 
import logging 
from twilio.rest import Client 

#Twilio Account Info 
account_sid = 'id'#not included for security 
auth_token = 'token'#not included for security 

client = Client(account_sid, auth_token) 
myTwilioNumber = '+myTwilioNumber'#not included for security 
myCellPhone = '+MyCellPhone'#not included for security 



##FUNCTIONS## 



#shutdown command 
def shutdown(): 
    os.system('shutdown -s') 

#restart command 
def restart(): 
    os.system('shutdown -r') 

#wake up computer and launch core programs 
def wakeup(): 
    subprocess.Popen('C:/Users/Thermaltake/AppData/Roaming/Spotify/Spotify.exe') 
    webbrowser.open('https://us-mg6.mail.yahoo.com/neo/launch') 
    webbrowser.open('https://www.rescuetime.com/dashboard') 

#Launch main coding applications 
def code(): 
    webbrowser.open('http://somafm.com/player/#/now-playing/groovesalad') 
    subprocess.Popen('C:\Windows\system32\cmd.exe') 
    subproces.Popen('C:\Python_Files\Sublime Text 3\sublime_text.exe') 

#Launch Remote Desktop and automatically log in 
def logmein(): 




    def main(): #main code 
     x=0 #counter 

     #checks for txt file in Remote_Control folder and assigns value to x 
     if os.path.isfile('C:/Users/Thermaltake/Dropbox/Remote_Control/shutdown.txt')==True: 
      x=1 
     elif os.path.isfile('C:/Users/Thermaltake/Dropbox/Remote_Control/wakeup.txt')==True: 
      x=2 
     elif os.path.isfile('C:/Users/Thermaltake/Dropbox/Remote_Control/restart.txt')==True: 
      x=3 
     elif os.path.isfile('C:/Users/Thermaltake/Dropbox/Remote_Control/code.txt')==True: 
      x=4 
     else: 
      print('No File Found') 

     #Checks x value and executes function 
     if x==1: 
      os.remove('C:/Users/Thermaltake/Dropbox/Remote_Control/shutdown.txt') 
      shutdown() 
      #print('Shutdown')#Placeholder for testing 
      message = client.messages.create(body='Shutdown Initiated', from_=, to=)#not included for security 
     elif x==2: 
      os.remove('C:/Users/Thermaltake/Dropbox/Remote_Control/wakeup.txt') 
      wakeup() 
      #print ('Spotify') #Placeholder. Define function to wake up PC and launch core programs 
      message = client.messages.create(body='Waking Up', from_=, to=)#not included for security 
     elif x==3: 
      os.remove('C:/Users/Thermaltake/Dropbox/Remote_Control/restart.txt') 
      restart() 
      #print ('Restart') #Placeholder. 
      message = client.messages.create(body='Restart Initiated', from_=, to=)#not included for security 
     elif x==4: 
      os.remove('C:/Users/Thermaltake/Dropbox/Remote_Control/code.txt') 
      code() 
      print('Happy Coding!') 
      message = client.messages.create(body='Ready to Code!', from_=, to=)#not included for security 
     else: 
      print ('No command entered') 

     #End Sequence (prints value and resets counter) 
     print (x) 
     x=0 

     os.system('pause') #Placeholder for testing 

if __name__ == '__main__': #Runs main function 
    main() 

''' 
TODO: 
Twilio not yet working. try different iterations of body and message. try assigning it to its own function 

subprocess failing to launch programs. research alternatives. SMTP is a possibility. 

possibly need to add Enter keystroke to the end of shutdown() and restart(). maybe even log in to log off. 

add 'send to-do list to phone' function. 

cleanup indentation and remove unnecessary modules 

add 'flvto' function 

add 'remote in' function. 


+ Version 1.1 7/6/17 + 

WORKING: 
subprocess.Popen('C:\Windows\system32\cmd.exe') 
webbrowser.open('http://somafm.com/player/#/now-playing/groovesalad') 
os.remove 
wakeup() 

NOT WORKING: 
subproces.Popen('C:\Python_Files\Sublime Text 3\sublime_text.exe') 
Twilio return message 
Task Scheduler: "Check_Dropbox on Lock" 

BUGS: 
IFTTT is buggy and sometimes unresponsive 
Task Scheduler to check file is too slow. Need something faster. (Possibly 'Watch 4 Folder') 
shutdown() waits til logon to initialize. 
restart() waits til logon to initialize. 

'''  

すべてのヘルプはなり非常に高く評価される。私はCSで正式な教育を受けていないことを覚えておいてください。しかし、今秋にCSのメジャーを始める前に、私は自分の足を濡らそうとしています。私は約3ヶ月間しかプログラミングしていません。

また、このコードに記載されている内容がさらにエレガントにできれば、喜んでアドバイスを受けていただきます。

-Jake

+3

'def logmein():'を削除します。なぜあなたはそれを持っていますか?そして、それはグローバルな範囲にあるように 'main'を捧げます。 –

+1

main()関数はlogmein()内で定義されているため、メインスクリプト – jeremycg

答えて

0

グローバルスコープに、そのようlogmein()内部からmain()を除去します。グローバルスコープからmainを呼び出すと、定義されていないからです。

+0

の範囲外です。ありがとう、ありがとうございます。私はlogmein関数を後で終えるつもりで書いていましたが、次の行がその関数内に包含されることに気付きませんでした。それは私が以前に字下げエラーを起こしていた理由を説明します。私はその行をコメントし、プログラムは正常に実行されます。本当にありがとう! –

関連する問題