2017-02-22 49 views
-3

我試圖在Python中響應True後將數據保存到文件。如果True使用Python將數據保存到文件

例如

a = 1 
if a = 1 save to file.txt 

callotherfunction後,我想響應,並檢查是否真正將它保存到一個文件:example.txt中

任何想法!

此致敬禮。

+0

所以你要數據字符寫入一個文件,如果它的值是真的嗎? –

+3

請閱讀[問]和[MCVE]。現在你的代碼片段甚至不是一個正確的Python(它引發'IndentationError')。 –

+0

即使這個問題不是很清楚! –

回答

0

你的問題不是很清楚,但基於什麼就有什麼,這裏有一個例子讓你開始:

# You stated 'after callotherfunction I want to get response'. 
# This is how you call another function and save the response to a variable. 
# Ensure you swap out `otherfunction` for the actual function name 
result = otherfunction() 

# You said you wanted to check if the result is `True` - this will explicitly check that. 
# However, your example is using the numeric `1`. 
# If you just want to check for a 'positive' (non zero, non empty, non null), 
# You can just do `if result:` 
if result == True: 
    # This is one way of opening a file and writing text to it in python 2.7 
    # There are other ways for more complicated outputs - e.g. the csv module 
    with open('path/to/output/file.txt', 'w') as fout: 
     fout.write('...') 
+0

謝謝你,這是我需要的。最好的祝福 –

相關問題