将Python变量传递给Subprocess

huangapple go评论117阅读模式
英文:

Pass Python variable to Suubprocess

问题

以下是您要翻译的内容:

我是Python的新手需要帮助解决以下问题

我试图使子进程执行以下代码以便存储在变量中的网站以Internet Explorer模式打开

link = "www.google.com"
def openlink(link: str):
    subprocess.call('powershell $ie = New-Object -com internetexplorer.application')
    subprocess.call('powershell', '$ie.navigate(link)')

当尝试运行时,我收到以下错误消息。

Traceback (most recent call last):
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\luiis\Desktop\IMPORTANTE\Softwares Luis Teixeira\Portal Links\main.py", line 10, in open
subprocess.call('powershell', '$ie.navigate(link)')
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 345, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 971, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1440, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] O sistema não conseguiu localizar o ficheiro especificado

希望这有所帮助。

英文:

I'm new to Python and I need help with the following problem.

I'm trying to make the subprocess execute the following code so that the sites stored in a variable open in Internet Explorer mode.

link = "www.google.com"
def openlink(link: str):
    subprocess.call('"powershell" $ie = New-Object -com internetexplorer.application')
    subprocess.call('powershell' '$ie.navigate(link)')

I get the following error when trying to run.

Traceback (most recent call last):
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\luiis\Desktop\IMPORTANTE\Softwares Luis Teixeira\Portal Links\main.py", line 10, in open
subprocess.call('powershell' '$ie.navigate(link)')
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 345, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 971, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\luiis\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1440, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] O sistema não conseguiu localizar o ficheiro especificado

答案1

得分: 2

I do not have internet explorer on my machine, but this code does not parse an error:

import subprocess

link = "www.google.com"

def openlink(link: str):
    subprocess.call(f'powershell "$ie = New-Object -com internetexplorer.application; $ie.navigate(\'{link}\')"' )

openlink(link)

OR you can have it bypass powershell by just opening with chrome or firefox directly like this:

import subprocess

link = "https://www.google.com"

def openlink(link: str):
    subprocess.call(f'start chrome {link}', shell=True)

openlink(link)
英文:

I do not have internet explorer on my machine, but this code does not parse an error:

import subprocess

link = "www.google.com"

def openlink(link: str):
    subprocess.call(f'powershell "$ie = New-Object -com internetexplorer.application; $ie.navigate(\'{link}\')"')

openlink(link)

OR you can have it bypass powershell by just opening with chrome or firefox directly like this:

import subprocess

link = "https://www.google.com"

def openlink(link: str):
    subprocess.call(f'start chrome {link}', shell=True)

openlink(link)

答案2

得分: 0

看起来你在尝试在Python脚本中执行PowerShell命令时遇到了文件未找到的错误。通常,我看到这个错误是因为subprocess模块无法找到“powershell”可执行文件。

要修复这个错误,你需要提供PowerShell可执行文件的完整路径。你可以通过修改第一个subprocess.call()行来实现:

subprocess.call(['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', '$ie = New-Object -com internetexplorer.application'])

这应该执行PowerShell命令并创建一个新的Internet Explorer应用程序。接下来,修改第二个subprocess.call()行为:

subprocess.call(['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', f'$ie.navigate("{link}")'])

这将使用f-strings将“link”变量的值插入到PowerShell命令中,使Internet Explorer导航到指定的URL。

请确保将“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”替换为你的PowerShell可执行文件的实际路径。

英文:

It looks like you are encountering a file not found error when you try to execute the PowerShell command in your Python script. Usually I've seen this error occurs when the subprocess module cannot find the "powershell" executable file.

To fix this error, you need to provide the full path to the PowerShell executable file. You can do this by modifying the first subprocess.call() line to:

subprocess.call(['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', '$ie = New-Object -com internetexplorer.application'])

This should execute the PowerShell command and create a new Internet Explorer application. Next, modify the second subprocess.call() line to:

subprocess.call(['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', f'$ie.navigate("{link}")'])

This will use f-strings to insert the value of the "link" variable into the PowerShell command, so that Internet Explorer will navigate to the specified URL.

Make sure to replace "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" with the actual path to your PowerShell executable file.

huangapple
  • 本文由 发表于 2023年3月31日 04:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定