I am trying to execute several python code at the same time in different folder using concurrent futures. The code run fine but when i check the subfolders I don't see any output so I am assuming the python code that I am calling were never executed.
The code first find a python script with specific name in all of the folders then call them in parallel. The write the output in another text file so my goal is to have them write the text file in folders that the script is located.
for example:
Folder A
ReadTextFileV9-10.py
Folder B
ReadTextFileV9-10.py
I want the out put to be
Folder A
ReadTextFileV9-10.py
Output.txt
Folder B
ReadTextFileV9-10.py
Output.txt
This is my code.
from concurrent import futures
import re
import os
import timeit
import os
import glob
import re
from glob import glob
import subprocess
FileList = [];
start_dir = os.getcwd();
pattern = "ReadTextFileV9-10.py"
for dir,_,_ in os.walk(start_dir):
FileList.extend(glob(os.path.join(dir,pattern))) ;
print(FileList)
def Run_Code(Str):
return subprocess.run(Str)
futuress=[]
def main_function(FileList):
with futures.ProcessPoolExecutor(max_workers=2) as executor:
#results = executor.map(Run_Code, FileList)
for file in FileList:
future = executor.submit(subprocess.run, file)
futuress.append(future)
return futuress
results=main_function(FileList)
I appreciate if any one can help me figure out what I am doing wrong.
Thanks
Related
I'm using python for a school project and i want to know how create a simple virus with simple this effect
i tried this:
How do I use shutil to make a python file copy itself after doing a calculation?
and this:
https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script
then i didn't know how to run the second copy
the code example(I WANT TO RUN THE COPY FOR ONCE)
import os
import shutil
##############
print("hello world")
shutil.copy(__file__, "copy.py") # Copies the file
exec(open("copy.py").read())# Executes the copied file if it is in the same directory as the other file
You can run the second file with execfile(File Path)
So you got something like
import os
import shutil
import fileinput
##############
print("hello world")
shutil.copy(__file__, "copy.py") # Copies the file
file = [Path of the copied File]
for line in fileinput.FileInput(file, inplace=1):
if "execfile('copy.py')" in line:
line=line.rstrip()
line=line.replace(line,"")
print (line,end="")
# This could get a problem with large files but should work properly with small files
execfile('copy.py') # Executes the copied file if it is in the same directory as the other file
# Else give it the directory where the file is stored (with / instead of \)
so i get it (i have posted in reddit too)
link: https://www.reddit.com/r/learnpython/comments/lxupy9/how_to_make_a_python_file_copy_itself_then/
import shutil
import subprocess
import os
import random
print("hello world")
new_file_name = str(random.randint(1, 10000)) + ".py"
shutil.copy(__file__, new_file_name)
subprocess.call((new_file_name), shell=True)
I have following the link:
to incorporate a command line command into my python script and it is working fine.
but i want o run the command over all the files present in a folder. How to send the file name to the command line? I think there should be some for loop but I cant hit the chord. Also I wnat to save the result in a .csv file.
import os
import subprocess
list_files = subprocess.run(["file","my_audio.wav"])
How to ?
for file_name in folder
output=subprocess.run(["file","file_name"])`
save output in .csv
Got the answer !
import os
import subprocess
import glob
with open("bit_rate.csv", "w") as fp:
for file_name in glob.glob('./*.wav'):
print(file_name)
subprocess.run(["file",file_name ], stdout=fp)
I've been trying to figure this out for hours with no luck. I have a list of directories that have subdirectories and other files of their own. I'm trying to traverse through all of them and move all of their content to a specific location. I tried shutil and glob but I couldn't get it to work. I even tried to run shell commands using subprocess.call and that also did not work either. I understand that it didn't work because I couldn't apply it properly but I couldn't find any solution that moves all contents of a directory to another.
files = glob.glob('Food101-AB/*/')
dest = 'Food-101/'
if not os.path.exists(dest):
os.makedirs(dest)
subprocess.call("mv Food101-AB/* Food-101/", shell=True)
# for child in files:
# shutil.move(child, dest)
I'm trying to move everything in Food101-AB to Food-101
shutil module of the standart library is the way to go:
>>> import shutil
>>> shutil.move("Food101-AB", "Food-101")
If you don't want to move Food101-AB folder itself, try using this:
import shutil
import os
for i in os.listdir("Food101-AB"):
shutil.move(os.path.join("Food101-AB", i), "Food-101")
For more information about move function:
https://docs.python.org/3/library/shutil.html#shutil.move
Try to change call function to run in order to retrieve the stdout, stderr and return code for your shell command:
from subprocess import run, CalledProcessError
source_dir = "full/path/to/src/folder"
dest_dir = "full/path/to/dest/folder"
try:
res = run(["mv", source_dir, dest_dir], check=True, capture_output=True)
except CalledProcessError as ex:
print(ex.stdout, ex.stderr, ex.returncode)
I am trying to get a list of the imported custom modules (modules I created by myself) in my current file but I could not find a proper way to achieve it.
For example:
<test.py>
import sys
import foo
import bar
I would like to get a list of [foo, bar] excluding the sys module.
Lets say, that if file located near curent file, or in some subfolder, when it's "our" module, not system one.
For this example, I've created three files: main.py, other.py and another.py, and placed whem into one folder.
The code of main.py is:
# os and sys are needed to work
import sys
import os
import shutil
import io
import datetime
import other
import another
def get_my_modules():
# Get list of modules loaded
modules = list(sys.modules.keys())
mymodules = []
# Get current dir
curdir = os.path.realpath(os.path.dirname(__file__))
for m in modules:
try:
# if some module's file path located in current folder or in some subfolder
# lets sey, that it's our self-made module
path = sys.modules[m].__file__
if path.startswith(curdir):
mymodules.append(m)
except Exception:
# Exception could happen if module doesn't have any __file__ property
pass
# Return list of our moudles
return mymodules
print(get_my_modules())
And this code actually outputs ["other", "another"]
This approach has a problem: If you import module, that somehow located in upper folder, it woudln't be detected.
I'm trying to write a function which will allow me move .xlsm files between folders. I'm aware I can use shutil.move(), however, I'm trying to build a function that will take a file path as a parameter/argument and then perform the procedure.
Here's what I have:
def FileMove(source):
import os
import shutil
source = 'C:\\Users\\FolderB\\'
archive = 'C:\\Users\\FolderA\\'
UsedFiles = os.listdir(source)
for file in UsedFiles:
shutil.move(source+file, archive)
This doesn't do anything. Just wondering if anyone could point me in the right direction
Cheers
So just delete def FileMove(source): and it works fine.
Or you do something like that:
import os
import shutil
def FileMove(source):
archive = 'C:\\Users\\FolderA\\'
UsedFiles = os.listdir(source)
for file in UsedFiles:
shutil.move(source+file, archive)
source = 'C:\\Users\\FolderB\\'
FileMove(source)