I'm trying to automate some file processing that occurs when you do data analysis on fastq files using STRaitRazor. Briefly, the files themselves are gzipped and the batch file looks into these contents, pulls out necessary information and then stores it in a new folder it creates. This works well manually but when I tried to use Python to call the batch file, the window opens for a split second and then disappears without running.
I've included the basic code I'm using here. There are no error messages but the batch file does not run. I've tried substituting "call" with "Popen" and "run" in the syntax and that also has not worked.
import subprocess
batch = "C:\\STRaitRazor\\Analysis\\fastq\\Data_2\\batchCstr8.bat"
subprocess.call([r"%s"%batch])
First off, I'd recommend calling this script from the command line to see what errors it outputs. But, as a guess, I'm assuming the batch file fails because it's not being run from the directory it expects to be run from, you'll want to change to it's directory first:
import subprocess
import os
batch = "C:\\STRaitRazor\\Analysis\\fastq\\Data_2\\batchCstr8.bat"
# Get the current directory, and change to the directory that batch file is in
previous_dir = os.getcwd()
os.chdir(os.path.dirname(batch))
# Run the batch file (note: don't need the % operator here, also, use check_call
# to raise an exception if calling the batch file fails)
subprocess.check_call([batch])
# Change back to the old working directory, not necessary, but helpful
# if there are any other steps to run after the batch file is done
os.chdir(previous_dir)
Related
I was earlier using console to run scripts which worked fine. Now that I wish to shift the entire process to crontab, I m facing ModuleNotFoundError:. The structure follows as I run a main file from parent directory, which imports another .py script from different location. To tackle the path error issue I have used os.chdir(path) which seems to work when I give print(os.getcwd()) but fails to find the required scripts in this new location. Can someone please help me with this. I m new to scripting.
#TIA
cwd = os.getcwd()
try:
os.chdir(cwd + 'pathToOtherScripts')
print('#\tGet current directory:',os.getcwd())
import SomeScript as SS
I get ModuleNotFoundError: at the parent directory. This error doesn't happen when I run the same script through console.
I have written some code in python with the help of selenium which is doing web crawling in which it goes to a website after entering login id and password and downloaded a file. I have written the code in jupyter notebook with file name as GSPL_Code.ipynb and with the use of !jupyter nbconvert --to script GSPL_Code.ipynb and !pyinstaller GSPL_Code.py, I have first converted .ipynb file to .py and then made a .exe file from the .py file.
If I run the .exe file which is named as GSPL_Code and it is stored in dist/GSPL_Code then a file crv_report.xlsx is downloaded in the dist/GSPL_Code folder where the executable file is also there but the problem is code does not executed after downloading a file in that location. It got stuck. Nothing is happening now. When I run in jupyter notebook then I don't face any problem but after converting it into .exe, I am facing the problem.
Please find the following python code which is part of the whole script:
def handler(driver):
curr=driver.current_window_handle
for handle in driver.window_handles:
driver.switch_to.window(handle)
if handle != curr:
driver.close()
for handle in driver.window_handles:
driver.switch_to.window(handle)
print(driver.current_url)
click_event('//img[#id="IconImg_crv_report_toptoolbar_export"]')
click_event('//div[#style="white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:249px"]')
click_event('//span[#title="Microsoft Excel Workbook Data-only"]')
sleep(5)
click_event('//td[#class="wizbutton"]')
sleep(15)
print("Report is downloaded")
PATH1="C:\\Users\\ankit19.gupta\\Desktop\\Test_GSPL\\dist\\GSPL_Code"
crv_report= "/crv_report.xlsx"
print("Reading File")
dataframe1 = pd.read_excel(PATH1+crv_report)
print("Data is stored in the dataframe")
return dataframe1
I have written some print statements to see where the error is coming. So, here I am unable to see the print statement "Report is downloaded". Before this click_event('//td[#class="wizbutton"]') is used to click a button which download the file in chrome browser and I can see this downloaded file in the path dist/GSPL_Code directory and it got executed successfully but after that it got stuck and nothing happened.
I am unable to find what is happening here. Can anyone please help me. Any help would be appreciated.
Edit: There were some delay due to which print statements "Report is not downloaded" and "Reading File" were not showing in console but now I am able to see it but it is unable to read the file crv_report which is on the path PATH1 and it got stuck here and due to which I am unable to see print statement Data is stored in the dataframe
I think code is unable to access the file because os.remove is also not working when I try to delete the crv_report file. Can anyone please help me.
OS Directory Change:
When refactoring python files, in your case changing .py to .exe, it's important to make sure everything is a part of the current working directory. Simple OS checker command:
cwd = os.getcwd()
If directories are misaligned, you will need to run an OS command to change the directory before interacting with any files:
os.chdir('/your/desired/directory')
I have a .tar.7z file which I want to unpack (when I unpack with archive_utility.app on MAC manually, I am getting a folder with subfolder and files).
I am not able to unpack this .tar.7z file with Python. Please let me know which module will let me do so.
i tried this using tarfile like below and failed to open it.
import tarfile
# open file
# file = tarfile.open('ckrseoselr7202-logs.tar.gz') # Working
file = tarfile.open('ckrseoselr7202-logs.tar.7z') # Not Working
# extracting file
file.extractall('./Folder')
file.close()
Same way i need to open .tar.7z file and want to read one particular file to parse the data. Let me know how i can achieve this using python3.
I want to convert the following sample python program into an executable file:
import os
print(os.getcwd())
To convert it into an executable I have used Pyinstaller:
pyinstaller app.py --onefile
And the EXE file is getting generated in the dist folder, but when I run it, it launches and immediately closes, and the expected print statement is not even displayed.
What could be the issue?
It does prints the statement, but just after printing it the code ends. You can add input() or use time.sleep(seconds) to make your program wait until you press a key or any particular number of seconds respectivly.
To check if your code(without the advice i have given) prints, start that python file in your command line.
Is it possible to create a Python Executable file (using PyInstaller or similar) that can in its code access other Python-files stored in a specific folder?
The reason for that it MUST be an Executable is that the script sometimes must be run from computers that has not it's own Python installed. I have no problem creating an executable (with PyInstaller for example) and they work fine. The python script itself loads various kinds of data into a database. But everytime there is a new kind of data that has to be loaded into the database I have to recreate the hole exe-file. I'm looking for a way for the executable to access python-files (with Data Load Instructions) so that the actual pyton load-files can be altered instead of the exe-file.
I have tried to find a solution using this:
import os, time
from subprocess import call
for file in os.listdir('.'):
if file == 'loadInstructions.py':
call(['python', file])
print(file)
cwd = os.getcwd()
print(cwd)
input('To EXIT progran press ENTER.')
time.sleep(3)
It works running it from a python editor but when I turn this into an exe-file it does not. If I creat an exe-file with "call(['python', file])" commented out the rest works which I interpret that the exe-file can find the file in question but not run it.
I would be gratefule for any help.