import time import subprocess #导入 subprocess 模块 import schedule import functools #要ping的地址和时间间隔 def main(): address_list = ['192.168.1.1','192.168.1.157','192.168.1.150'] for address in address_list: ping_address(address) interval =1 #每60秒执行一次 #定义一个函数,接收一个命令字符串作为参数 def run_cmd(cmd): #打开一个新的CMD窗口 process = subprocess.Popen( 'cmd.exe', stdin = subprocess.PIPE, #标准输入流 stdout = subprocess.PIPE, #标准输出流 stderr = subprocess.PIPE, #标准错误流 shell =True, #在CMD窗口中执行命令 ) #向CMD窗口发送命令 process.stdin.write(cmd.encode('GBK')+b'\n') #读取CMD窗口的输出 output,error = process.communicate() #如果出现了错误,输出错误信息 if error: print(error.decode('GBK')) #输出命令的执行结果 result = output.decode('GBK') return result #定义一个函数,用于定时执行PING命令 def ping_address(address): for i in range(3): cmd = f'ping {address}' result=run_cmd(cmd) print(result) #使用schedule 模块创建一个定时任务,每隔interval 秒执行一次 ping_address 函数 for address in ['192.168.1.1','192.168.1.157','192.168.1.150']: schedule.every(interval).seconds.do(functools.partial(ping_address, address)) #主循环,保持程序运行 while True: schedule.run_pending() time.sleep(1)这段代码有没有冗余的部分
时间: 2023-05-19 09:06:59 浏览: 161
这是Python中的两个模块,分别是时间模块和子进程模块。
time模块提供了一些与时间相关的函数,比如获取当前时间、时间戳转换等。
subprocess模块则提供了一些创建和控制子进程的函数,比如执行外部命令、获取命令输出等。
相关问题
关于subprocess模块在Windows系统上的代码,以下说法正确的是 import subprocess def show_file(file_name) return subprocess.check_output('[notepad.exe',file_name],shell=False) if_name_ == "_main_": _out = show_file("d:/abc.txt&&calc.exe")
以上代码存在以下问题:
1. `subprocess.check_output` 函数的参数应该是一个字符串列表,而不是一个字符串。应该修改为 `[notepad.exe', file_name]`。
2. 在 Windows 系统上,命令行中使用的路径分隔符是反斜杠 `\`,而不是正斜杠 `/`。应该将路径中的 `/` 修改为 `\`。
3. `_out = show_file("d:/abc.txt&&calc.exe")` 这条语句的意思是打开文件 `d:/abc.txt` 并且执行计算器程序 `calc.exe`,但是在 Windows 系统上使用 `&&` 连接命令的方式是错误的,正确的方式是使用 `&` 连接。
因此,正确的代码应该是:
```
import subprocess
def show_file(file_name):
return subprocess.check_output(['notepad.exe', file_name], shell=False)
if __name__ == "__main__":
_out = show_file("d:\\abc.txt & calc.exe")
```
import subprocess def show_file(file_name) return subprocess.check_output('[notepad.exe',file_name],shell=False) if_name_ == "_main_": _out = show_file("d:/abc.txt&&calc.exe")的运行结果
代码中存在语法错误,应该是:
```
import subprocess
def show_file(file_name):
return subprocess.check_output(['notepad.exe', file_name], shell=False)
if __name__ == "__main__":
_out = show_file("d:/abc.txt&&calc.exe")
```
在这个程序中,`show_file()`函数使用`subprocess.check_output()`函数来打开指定的文件。但是,在`_out = show_file("d:/abc.txt&&calc.exe")`这行代码中,尝试同时打开`abc.txt`和`calc.exe`,这是不被允许的,并且会导致`subprocess.check_output()`函数抛出异常。正确的调用方式应该是:
```
_out = show_file("d:/abc.txt")
```
这将打开`abc.txt`文件并使用默认的文本编辑器进行编辑。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)