簡體   English   中英

random.choices 模塊“lambda_function”中的語法錯誤?

[英]Syntax error in module 'lambda_function' for random.choices?

我是 AWS Lambda 的新手,我不知道我的代碼有什么問題。 當我運行這個:

def chooseRndBook():
    os.chdir("sources") #the folder name which has the .txt files
    dir = os.getcwd()
    bookSet = os.listdir(dir)
    try:
        bookSet.remove('.DS_Store')

    #to randomly choose a book from the directory
    selectedBookFile = random.choice(bookSet)
    return selectedBookFile

我收到此錯誤:


  "errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 37)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 37\n        selectedBookFile = random.choice(bookSet)\n"
  ]
}

我究竟做錯了什么? 它在我的設備上完美運行,但在 AWS smh 上運行不佳。 我正在嘗試獲取文件夾中 all.txt 文件的列表,列出它們,然后 select 是該列表中的一個隨機元素。

你的問題不在於random.choice ,而是在無效的try/except塊中,它缺少except塊。 它應該是這樣的:

...
try:
  bookSet.remove('.DS_Store')
except:
  # Here implement error handler

selectedBookFile = random.choice(bookSet)
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM