LF> os.system(filePath) alternative - python

I'm having an issue due to the fact that in my Python code os.system(filePath) will execute but the code stops until I manually close the window of the opened PDF file.
I would like to open a PDF file use openCV to capture my screen thus capturing the image of the file. This becomes an issue if the code does not run without me closing the window...
CODE:
import numpy as np
import shutil,os, cv2, SendKeys, win32com.client
#While our In File has stuff in it
while(os.listdir(pathForInFile)!=[]):
for subdir, dirs, inFile in os.walk(pathForInFile):
for fileName in inFile:
filePath= subdir+os.sep+fileName
os.system(filePath)
print "THIS WILL RUN NOT UNTIL I CLOSE FILE"
shell.SendKeys('{DOWN}')

You can use this solution both on Windows 8.1 (tested in a virtual machine):
import os
os.system('start path_to_my_filename.pdf')
print("You do not need to close the opened PDF file to run and see this")
and Ubuntu (14.04.4 LTS):
import os
# f = /home/begueradj/Desktop/metasploitBible.pdf'
os.system('xdg-open path_to_my_filename.pdf')
print("You do not need to close the opened PDF file to run and see this")

Related

Open last saved CSV file via python

I have a file to be downloaded in CSV format every day that it's name changes daily. how can i open that file via python in Excel after being downloaded automatically?
I've tried the below solution but i have to mention the file name in full which I can't do because it changes dynamically.
from subprocess import Popen
p = Popen('filename.csv', shell=True)
At the end of your code that downloads the file You can find latest file in your download folder as and open it
import glob
import os
from subprocess import Popen
list_of_files = glob.glob('/path_to_download_folder/*.csv')
latest_csv_file = max(list_of_files, key=os.path.getctime)
print(latest_csv_file)
#then do what ever you want to do with it.
os.startfile(latest_csv_file)

Different output in different exe files Python

I have been working on an automation script which includes shutting down my Windows 10 pro machine.
Here is one Python code which turned to exe file
import os
import time as t
import re
while True:
To check for "sd" in a file
lst = os.listdir("attachments")[-1]
with open(f"attachments\\{lst}", "r") as f:
message = f.read()
x = re.search("sd", message)
if "sd" is found then the file must be deleted and the computer must be shut down
if x:
print("YESSSS")
os.remove(f"attachments\\{lst}")
os.system("shutdown /s /t 1")
t.sleep(5)
The problem is that after deleting the file instead of shutting down the PC the cmd.exe appears on my screen...idk its weird
click for the image
to check if the exe format didnt have any problems i created another exe file
here is the code
import os
os.system("shutdown /s /t 1")
Turns out there is nothing wrong with the exe format...it performs normal shutdown....god just doesn't like me
Please help me 🥺

Include poppler in exe file with pyinstaller

I tried to write a simple program for converting PDF files to JPG. The main idea is a simple .exe file that will find all PDF files in the same directory, and after converting them to JPG put them in the new folder.
And I did it! Using pdf2image, my program doing exactly as I want and as .py and as .exe. But, after running this program on another PC I got an error with popper >following Cmd screenshot
So, I understand, the program tries to find a popper. And sure on another PC program can't find it.
I tried --hidden-import, and --onedir, --onefile e.t.c.
Also saw some similar problem here, as:
PyInstaller and Poppler
Include poppler while generating an application using pyinstaller
But, or I do something wrong, or can't clearly understand how to do solutions in this questions.
What should I do?
#P.S. Maybe, there is a better library or module to create this kind of program?
Whole code:
import os
import pdf2image
from pdf2image import convert_from_path
from inspect import getsourcefile
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
# Create output direction
# Direction of executable file
script_dir = os.path.abspath(getsourcefile(lambda:0))
# print(script_dir)
output_dir = 'JPG'
# List for files which wasn't converted
error_list = []
# If path don't exist - create
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#print('Path has been created', "\n")
else:
pass
#print('Directory exist', "\n")
# Show all files in directory
file_list = os.listdir()
print(file_list, "List of files")
for file in file_list:
try:
# print(file)
pages = convert_from_path(file, dpi=300, fmt='jpg', output_folder=output_dir, output_file=file)
except Exception as e: print(e)
print("File wasn't converted:", "\n")
if len(error_list) == 0:
print(0, "\n")
else:
for f in error_list:
print(f)
input("Done! Press Enter")

Python executable running files with associated extension

I have compiled my python program with cx_Freeze with the lines
import sys
print(sys.argv[0])
to get the name of the extension file that runs my application.
I want to be able to double click on a file named Foo.bas and then my compiled executable starts and it can open the file and read its contents. So I want to get the extension path and file name and read its contents like this
with open(file, "r") as f:
data = f.read()
# do things with contents
where file would be the extension path and name
So how would I do that?
sys.argv[0] gives you the first entry of the command used to run your script, which is the script name itself. If you double-click on a file whose extension is associated with your script or frozen application, the name of this file becomes the second argument of the command, which is available through sys.argv[1]. See for example sys.argv[1] meaning in script.
So try with the following script:
import os
import sys
if len(sys.argv) > 1:
filename = sys.argv[1]
print('Trying with', filename)
if os.path.isfile(filename):
with open(filename, 'r') as f:
data = f.read()
# do things with contents
else:
print('No arguments provided.')
input('Press Enter to end')
This works both as unfrozen script and as executable frozen with cx_Freeze. On Windows you can drag and drop your Foo.bas file onto the icon of your script or executable, or right-click on Foo.bas, chose Open with and select your script or executable as application.

Shutil.move python script not executing or moving files

So, I've been trying to write a python script to move .jpg's from one file to another and although the program itself (seemingly) runs to completion as it prints "Task completed", the files do not actually move from one folder to the other. This is the script:
import shutil
from os.path import join
import os
source = join('C','Users','Francisco','Desktop','Test')
destination1 = join('C','Users','Francisco','Desktop','Archive1')
for files in source:
if files.endswith(".JPG"):
shutil.move(source,destination1)
print("Task completed");
I've tried running the script through both Command Prompt and the IDLE editor module, and even have Python 3.x set as a path in my Environment Variables, but nothing seems to work. I've become very frustrated by being unable to move the images from one folder to another and would like to see if you guy could help me figure out what the issue is here, be it a problem with the script itself or with the python software on my computer.
I'm a beginner at scripting with python so any help would be greatly appreciated. Thanks in advance.
You're looping through your source path characters, not through the actual folder, a quick fix:
import os
import shutil
source = os.path.join('C:\\', 'Users', 'Francisco', 'Desktop', 'Test')
destination1 = os.path.join('C:\\', 'Users', 'Francisco', 'Desktop', 'Archive1')
for filename in os.listdir(source):
if filename.lower().endswith(".jpg"):
shutil.move(os.path.join(source, filename), destination1)
print("Task completed")
You've got some bugs in your code:
import shutil
from os.path import join
import os
source_dir = join('C:/','Users','Francisco','Desktop','Test')
dest = join('C:/','Users','Francisco','Desktop','Archive1')
files = os.listdir(source_dir)
for source in files:
if source.endswith(".JPG"):
shutil.move(source, dest)
print("Task completed");

Categories