顯示具有 Python 標籤的文章。 顯示所有文章
顯示具有 Python 標籤的文章。 顯示所有文章

2024年11月7日 星期四

Python的os.listdir("/")列出來的東西,其中.file, "."是啥意思?

 在 Python 中,os.listdir("/") 列出指定目錄(此處為根目錄 /)中的所有項目。對於你提到的:

  • .file:如果有這樣的名稱,它是檔案或目錄名稱中以 . 開頭的文件或目錄。以 . 開頭的檔案或目錄通常被稱為隱藏檔案,在很多作業系統中不會在預設狀態下顯示出來,因為它們通常是設定檔或系統檔。


有.開頭是隱藏檔案,但是沒.開頭也可能是隱藏檔案。

2022年7月14日 星期四

How to terminate a python subprocess launched with shell=True (轉貼)

I'm launching a subprocess with the following command:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

However, when I try to kill using:

p.terminate()

or

p.kill()

The command keeps running in the background, so I was wondering how can I actually terminate the process.

Note that when I run the command with:

p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)

It does terminate successfully when issuing the p.terminate().

11 Answers

501

Use a process group so as to enable sending a signal to all the process in the groups. For that, you should attach a session id to the parent process of the spawned/child processes, which is a shell in your case. This will make it the group leader of the processes. So now, when a signal is sent to the process group leader, it's transmitted to all of the child processes of this group.

Here's the code:

import os
import signal
import subprocess

# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before  exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                       shell=True, preexec_fn=os.setsid) 

os.killpg(os.getpgid(pro.pid), signal.SIGTERM)  # Send the signal to all the process groups

2022年1月14日 星期五

What is arguments[0] while invoking execute_script() method through WebDriver instance through Selenium and Python?(轉貼)

Ref: https://stackoverflow.com/questions/52273298/what-is-arguments0-while-invoking-execute-script-method-through-webdriver-in/52280298#52280298

 I'm trying to crawl the pages that I interested in. For this, I need to remove attribute of element from HTML. 'style' is what I want to remove. So I find some codes from Stackoverflow.(i'm using Chrome for driver)

element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")
driver.execute_script("arguments[0].removeAttribute('style')", element)

What does arguments[0] do in the code? Can anyone explain arguments[0]'s roles concretely?


Ans:

arguments is what you're passing from Python to JavaScript that you want to execute.

driver.execute_script("arguments[0].removeAttribute('style')", element) 

means that you want to "replace" arguments[0] with WebElement stored in element variable.

This is the same as if you defined that element in JavaScript:

driver.execute_script("document.querySelector('select.m-tcol-c#searchBy').removeAttribute('style')")

You can also pass more arguments as

driver.execute_script("arguments[0].removeAttribute(arguments[1])", element, "style")
  • Note the word WebElement to be passed to the execute_scriptmethod. Passing the result from element(by.css(...)) directly causes CallStackSize error, that's why one has to use element(by.css(...)).getWebElement()  Jun 3 '21 at 12:57