使用FileSystemWatcher的PowerShell脚本不会启动Python scrip

2024-03-29 13:42:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我认为这个问题与FileSystemWatcher作为系统帐户运行有关,即使.ps1脚本只需右键单击并选择“runwithpowershell”即可启动。你知道吗

我尝试了几种方法,将命令集成到主.ps1脚本中,并在操作事件期间启动另一个.ps1脚本。你知道吗

两者都执行后端功能或移动\命名\删除和检索文件内容,但在执行.py脚本时,它不会在当前会话中启动。你知道吗

有人知道如何让窗口在前台启动吗? Python代码运行一个插件Selenium,需要打开Chrome来完成它的工作。你知道吗

一如既往地感谢大家!你知道吗

代码如下:

$folder = 'C:\scripts\tmp' # root path to monitor
$filter = '*.*'  # wildcard filter

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
Out-File -FilePath C:\Scripts\URLwatcher\logURL.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"

#Create a timestamp var
$filetimestamp = (Get-Date -Format yyyy-mm-dd-hhmmss)

#Create a timestamp file name
$timestampedURLfile = "C:\Scripts\URLwatcher\processing\" + $filetimestamp + ".txt"

#Move URL file and rename it
move-item C:\scripts\tmp\data.txt $timestampedURLfile

#Extract & store the URL from the file
$addURL = Get-Content $filename

#Scrape the url
python C:\Scripts\DailyStats.py -l $addURL  #### <--This script will not run ####

#Delete the URL file
del $filename
}

Tags: thenamepytxt脚本eventurlscripts
1条回答
网友
1楼 · 发布于 2024-03-29 13:42:40

好吧,我想出来了。 为了让它工作,我不得不使用Start进程来启动另一个.ps1脚本。你知道吗

至于让.py脚本启动,我将目录更改为脚本所在的位置,并从那里执行它,如下所示。你知道吗

start-process powershell C:\Scripts\URLwatcher\ManualScrape.ps1
                                

#Debugger \\ Set-PSDebug -Trace 1

#Create a timestamp var
$movetimestamp = (Get-Date -Format yyyy-mm-dd-hhmmss)

#Create a timestamp file name
$timestampedURLfile = "C:\Scripts\URLwatcher\processing\" + $movetimestamp + ".txt"

#Move URL file and rename it
move-item C:\Scripts\tmp\data.txt $timestampedURLfile

#Extract & store the URL from the file
$addURL = Get-Content $timestampedURLfile

#Run the scraper
cd C:\Scripts
python.exe DailyStats.py -l $addURL

#Delete the URL file
del $timestampedURLfile

相关问题 更多 >