【问题标题】:Python timed subprocess.PopenPython定时子进程.Popen
【发布时间】:2014-09-15 06:27:16
【问题描述】:

我有以下代码从网络爬虫中获取数据。 我才刚刚学会如何使用

subprocess.Popen

我正在尝试使用我的主动性以及其他关于如何使用的类似问题的答案

subprocess.Popen

执行下面的脚本,将 webscrape 数据放入我的插入字段,每 30 秒左右更新一次。但它不起作用。请你指出我正确的方向吗?

import xlrd
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk

class Application(Frame):
    """GUI to display results of 'equity get'"""
    
    def __init__(self, master):
        """initialise the Frame"""
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()
 
    def create_widgets(self):
        """Create button, text and entry Widget"""
        """what it is i.e. label"""
        
        url = "https://......."
        request= urllib2.Request(url)
        handle = urllib2.urlopen(request)
        content = handle.read()
        splitted_page = content.split("<.......">", 1);
        splitted_page = splitted_page24[1].split("</.......>", 1)

        self.data = Label(self, text ="Data")
        self.data1 = Entry(self, width = 10)
        self.data1.insert(0,splitted_page[0])

        self.data.grid(column = 1, row = 1)
        self.data1.grid(column = 2, row = 1)
        self.data1.grid(column = 3, row = 1)            

        a = 0
        while a < 10:
            a += 1
            time.sleep(15)
        while True:
            out =   subprocess.Popen(["C:\Users\.....\Desktop\Py\python.exe","C:\Users\.....\Desktop\..\Python27\.....\tester.py"])

app = Application(root)
root.title("reload test")
root.geometry("700x300")
root.mainloop()

我得到的错误是 错误号 22:引用之间的脚本的语法无效

     (["C:\Users\.....\Desktop\Py\python.exe","C:\Users\.....\Desktop\..\Python27\.....\tester.py"])

然后打开多个命令行窗口显示相同的错误,我必须关闭计算机才能停止它!

我用“r”前缀修改了对我的文件的引用,如下所示:

([r"C:\Users\.....\Desktop\..\Python27\.....\tester.py"])

但删除了 python.exe 调用,因为它只是调用命令行窗口。现在,我收到以下错误消息:

Traceback (most recent call last):
File "C:\Users\....\Desktop\Py\Python27\.....\tester.py", line 46, in <module>
app = Application(root)
File "C:\Users\......\Desktop\Py\Python27\.....\tester.py", line 18, in __init__
self.create_widgets()
File "C:\Users\.....\Desktop\Py\Python27\......\tester.py", line 44, in create_widgets
out = subprocess.Popen([r"C:\Users\Isaac\Desktop\Py\Python27\.....\tester.py"])
File "C:\Users\.....\Desktop\Py\lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\Users\.....\Desktop\Py\lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application

【问题讨论】:

  • 它为什么不能正常工作?你有错误吗?告诉我们它是什么,如果有的话。
  • 尝试将自己限制为每个问题一个固定错误,即,将 "%1 is not an valid Win32 application" 发布为另一个问题。您可以使用sys.executable 而不是指定r'...\pythonw.exe'(假设您使用pythonw.exe(GUI,而不是控制台应用程序)运行您的父脚本)。

标签: python loops timer subprocess reload


【解决方案1】:

Python 使用反斜杠来引用字符,例如 \n = newline 和 \t = tab。

使用 r 前缀生成原始字符串文字,例如 Windows 文件路径:

out =  subprocess.Popen([r"C:\Users\.....\Desktop\Py\python.exe", r"C:\Users\.....\Desktop\..\Python27\.....\tester.py"])

【讨论】:

  • 但不幸的是,这不会每秒重复或刷新我的脚本!
  • @isaac1290 请作为另一个问题重新发布,以便人们回答。 (提示:考虑 subprocess.check_outputPopen
猜你喜欢
  • 2018-12-05
  • 1970-01-01
  • 2012-03-03
  • 2011-08-05
  • 2017-02-03
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
相关资源
最近更新 更多