Python get all open files

lebedov / lsof_funcs.py

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

#!/usr/bin/env python
«»»
Python functions for finding open files and PIDs that have opened a file.
«»»
import numbers
import subprocess
try :
from subprocess import DEVNULL
except ImportError :
import os
DEVNULL = open ( os . devnull , ‘wb’ )
def get_open_files ( * pids ):
«»»
Find files opened by specified process ID(s).
Parameters
———-
pids : list of int
Process IDs.
Returns
——-
files : list of str
Open file names.
«»»
for pid in pids :
if not isinstance ( pid , numbers . Integral ):
raise ValueError ( ‘invalid PID’ )
files = set ()
for pid in pids :
try :
out = subprocess . check_output ([ ‘lsof’ , ‘-wXFn’ , ‘+p’ , str ( pid )],
stderr = DEVNULL )
except :
pass
else :
lines = out . strip (). split ( ‘ \n ‘ )
for line in lines :
# Skip sockets, pipes, etc.:
if line . startswith ( ‘n’ ) and line [ 1 ] == ‘/’ :
files . add ( line [ 1 :])
return list ( files )
def get_pids_open ( * files ):
«»»
Find processes with open handles for the specified file(s).
Parameters
———-
files : list of str
File paths.
Returns
——-
pids : list of int
Process IDs with open handles to the specified files.
«»»
for f in files :
if not isinstance ( f , basestring ):
raise ValueError ( ‘invalid file name %s’ % f )
pids = set ()
try :
out = subprocess . check_output ([ ‘lsof’ , ‘+wt’ ] + list ( files ),
stderr = DEVNULL )
except Exception as e :
out = str ( e . output )
if not out . strip ():
return []
lines = out . strip (). split ( ‘ \n ‘ )
for line in lines :
pids . add ( int ( line ))
return list ( pids )
Читайте также:  Css проценты или пиксели

python3-fix for get_pids_open

def get_pids_open(*files): """ Find processes with open handles for the specified file(s). Parameters ---------- files : list of str File paths. Returns ------- pids : list of int Process IDs with open handles to the specified files. """ for f in files: if not isinstance(f, str): raise ValueError('invalid file name %s' % f) pids = set() try: out = subprocess.check_output(['lsof', '+wt']+list(files), stderr=DEVNULL).decode('utf8') except Exception as e: out = e.output.decode('utf8') if not out.strip(): return [] lines = out.strip().split('\n') for line in lines: pids.add(int(line)) return list(pids) 

Источник

Open All the Files in a Directory in Python

Open All the Files in a Directory in Python

  1. Open All Files in a Folder/Dictionary Using os.walk() in Python
  2. Open All the Files in a Folder/Directory With the os.listdir() Function in Python
  3. Open All the Files in a Folder/Directory With the glob.glob() Function in Python

You can mainly use three methods to open all files inside a directory in Python: the os.listdir() function, os.walk() function and the glob.glob() function. This tutorial will introduce the methods to open all the files in a directory in Python. We’ve also included program examples you can follow.

Open All Files in a Folder/Dictionary Using os.walk() in Python

Various OS modules in Python programming allow multiple methods to interact with the file system. It has a walk() function that will enable us to list all the files in a specific path by traversing the directory either bottom-up or top-down and returning three tuples — root, dir, and files.

In the above syntax, r is to read the root folder or directory, and the parameter pathname is the path of the folder.

import os for root, dirs, files in os.walk(r'/content/drive/MyDrive/Skin Cancer'):  for file in files:  if file.endswith('.zip'):  print(os.path.join(root, file)) 

In the code, we first imported the OS module. Then in the read mode, we used a for loop and passed the pathname to the walk function.

Читайте также:  Javascript atom как запустить код

The loop iterates through all files that meet the file extension condition. The above code will read all files with a .zip extension.

/content/drive/MyDrive/Skin Cancer/archive.zip 

As you can see, the Google drive Skin Cancer folder contains one zip file.

Open All the Files in a Folder/Directory With the os.listdir() Function in Python

The listdir() function inside the os module is used to list all the files inside a specified directory. This function takes the specified directory path as an input parameter and returns the names of all the files inside that directory. We can iterate through all the files inside a specific directory using the os.listdir() function and open them with the open() function in Python.

The following code example shows us how we can open all the files in a directory with the os.listdir() and open() functions.

import os  for filename in os.listdir("files"):  with open(os.path.join("files", filename), 'r') as f:  text = f.read()  print(text) 
This is the first file. This is the second file. This is the last file. 

We read the text from the three files inside the files/ directory and printed it on the terminal in the code above. We first used a for/in loop with the os.listdir() function to iterate through each file found inside the files directory. We then opened each file in read mode with the open() function and printed the text inside each file.

Open All the Files in a Folder/Directory With the glob.glob() Function in Python

The glob module is used for listing files inside a specific directory. The glob() function inside the glob module is used to get a list of files or subdirectories matching a specified pattern inside a specified directory. The glob.glob() function takes the pattern as an input parameter and returns a list of files and subdirectories inside the specified directory.

We can iterate through all the text files inside a specific directory using the glob.glob() function and open them with the open() function in Python. The following code example shows us how we can open all files in a directory with the glob.glob() and open() functions:

import glob import os for filename in glob.glob('files\*.txt'):  with open(os.path.join(os.getcwd(), filename), 'r') as f:  text = f.read()  print(text) 
This is the first file. This is the second file. This is the last file. 

We read the text from the three files inside the files/ directory and printed it on the terminal in the code above. We first used a for/in loop with the glob.glob() function to iterate through each file found inside the files directory. We then opened each file in read mode with the open() function and printed the text inside each file.

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

Related Article — Python File

Related Article — Python Directory

Copyright © 2023. All right reserved

Источник

Оцените статью