如何在Python中从文本文件传递输入参数?

时间:2019-03-13 19:00:52

标签: python input parameters python-2.x

我一直在使用GUI计时器,其中必须在参数中指定时间。但是时间会有所不同,所以我想从文本文件中输入内容。

代码取自第一个答案。

Making a countdown timer with Python and Tkinter?

我尝试过的事情:

f = open('f.txt', 'r')
data = f.read()     
f.close()               
print data  
s = 'data'
self.countdown(s)

f.txt文件的数字为10。

但由于数据类型无法正常工作,并出现以下错误:

Traceback (most recent call last):
File "new.py", line 33, in <module>
app = ExampleApp()
File "new.py", line 15, in __init__
self.countdown(s)
File "new.py", line 28, in countdown
self.label.configure(text="%d" % self.remaining)
TypeError: %d format: a number is required, not str

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

使用''时,您给出的是字符串,而不是变量。
另外,您可能希望将值读取为数字。 这可能就是您想要的:

import concurrent.futures
import urllib.request

URLS = ['http://www.foxnews.com/',
        'http://www.cnn.com/',
        'http://europe.wsj.com/',
        'http://www.bbc.co.uk/',
        'http://some-made-up-domain.com/']

# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r page is %d bytes' % (url, len(data)))