I have tried this code in pyhthon shell prompt using windows. but I am getting the error as follows
>>> python -m twobitreader hg19.2bit < example.bed
SyntaxError: invalid syntax
I have also tried with the code you have sent
import twobitreader
with open("fas.fa", "w") as output, open('example.bed') as bedfile:
twobit = twobitreader.TwoBitFile('hg19.2bit')
twobitreader.twobit_reader(twobit, input_stream=bedfile, write=output)
When I try to execute the above code I am getting error as
Traceback (most recent call last):
File "D:/genome/sample6.py", line 3, in <module>
with open("fas.fa", "w") as output, open('example.bed') as bedfile:
IOError: [Errno 2] No such file or directory: 'example.bed'`Filed:
I unable to trace the error exactly.
You are trying to execute a shell command in the Python interpreter. The interpreter is not a shell.
Inside the shell you can still execute the same code the command line code would. Both a 2bit file and the input stream is be required for twobit_reader() to do anything. The first line of the function reads:
if input_stream is None: return
The library takes the hg19.2bit file as a TwoBitFile object; the input_stream argument must be a file or other iterable in using the BED format, according to the documentation string for the command line tool:
Regions should be given in BED format on stdin
chrom start(0-based) end(0-based, not included)
To use a BED file of regions, do
python -m twobitreader example.2bit < example.bed
From a Python script, the example.bed input should be an open file passed in as input_stream:
import twobitreader
with open("fas.fa", "w") as output, open('example.bed') as bedfile:
twobit = twobitreader.TwoBitFile('hg19.2bit')
twobitreader.twobit_reader(twobit, input_stream=bedfile, write=output)
The project documentation provides a link for the BED format: http://genome.ucsc.edu/FAQ/FAQformat#format1
Related
When I try to run my batch file for python script I get the error
D:\Sandbox\Python\Python Scripts>AQACompareBooks.py "D:/Sandbox/AQA/OrderAnalysis/Customer 1 OrderAnalysis -1 2018-05-08.xlsx"
[]
Traceback (most recent call last):
File "D:\Sandbox\Python\Python Scripts\AQACompareBooks.py", line 33, in <module>
xls = pd.ExcelFile(file)
File "D:\Dev\Sandbox\Python\lib\site-packages\pandas\io\excel.py", line 260, in __init__
self.book = xlrd.open_workbook(io)
File "D:\Dev\Sandbox\Python\lib\site-packages\xlrd\__init__.py", line 116, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: '[]'
Batch File
AQACompareBooks.py "D:\Sandbox\AQA\OrderAnalysis\Customer1 OrderAnalysis -1 2018-05-08.xlsx"
Python Code
import pandas as pd
import numpy as np
import os
import sys
print(sys.argv[1:])
file = format(sys.argv[1:])
xls = pd.ExcelFile(file)
If I set the path manually in the code I everything works fine.
i.e.
file = 'D:\Sandbox\AQA\OrderAnalysis\Customer1 OrderAnalysis -1 2018-05-08.xlsx'
Has anyone experienced this issue before?
Batch file and script are in the same folder.
The files I want to load is not
This looks a lot like it's a Windows issue with the extension association for .py files. In one Windows installation I checked where a test Python script did receive arguments, the registry key HKEY_CLASSES_ROOT\Python.File\Shell\open\command is set to "C:\WINDOWS\py.exe" "%L" %* (including those quotes). The trailing %* is what passes the arguments that Python then stores in sys.argv.
Second is the part with format(sys.argv[1:]); that slice will always produce a list, whereas the filename you wanted is stored in the single element sys.argv[1]. Simply use that instead.
With thanks to tdelaney for pointing them out, the associations can be checked (and, if administrator, set) using the assoc and ftype commands:
C:\Users\yann>assoc .py
.py=Python.File
C:\Users\yann>ftype Python.File
Python.File="C:\WINDOWS\py.exe" "%L" %*
I have a script called client that does a few things around the OS and it's failing when it comes to /proc/1/cwd
I'm trying to ignore these types of files but the script is still crashing when it comes to this file. I get Memory Map output and also a Stack Trace before the script crashes.
I did a file /proc/1/cwd command on the file and I can see it's a symlink, where I then included an if os.path.islink(file) is False: IF statement to skip it and proceed.
However, I seem to still be hitting it.
The below is the Traceback after the crash.
Traceback (most recent call last):
File "client.py", line 235, in <module>
File "client.py", line 120, in runner
OSError: [Errno 13] Permission denied: '/proc/1/cwd'
Failed to execute script client
The error suggests the script client is working on /proc/1/cwd and then right after seems to say that it can't execute itself?
Any help would be appreciated.
-----Additional -------
Code lines from error:
like 235 is literally the line runner(path)
runner() represents a function that has a for loop that cycles through files in a folder to find the correct file. During this process, runner will do the following:
do line 120 mode = os.stat(fullfilename).st_mode
check if os.path.isdir(fullfilename) is False:
then if stat.S_ISREG(mode) is True:
then if os.path.islink(fullfilename) is False:
then if fullfilename not in blacklist:
Line 120 is mode = os.stat(fullfilename).st_mode
def functION():
Source_obj = path.relpath("WebSource\EXAMPLE SOURCE.htm")
data = Source_obj.read()
I am having trouble opening this file while located in a sub-directory directly underneath my Python file... is there a better way to open files from ANY directory on my computer?
FileNotFoundError: [Errno 2] No such file or directory: 'WebSource\\EXAMPLE SOURCE.htm'
I can't read from the file because I get the following error:
C:\python34\python.exe G:\Robot\test.py
Process started >>>
Traceback (most recent call last):
File "G:\Robot\test.py", line 118, in <module>
functION()
File "G:\Robot\test.py", line 64, in functION
data = Source_obj.read()
AttributeError: 'str' object has no attribute 'read'
<<< Process finished. (Exit code 1)
================ READY ================
BTW: The file to be read is just a source file from an HTML Chrome webpage.
EDIT
I'm looking for more help with the path and wondering why I get the first mentioned Traceback regarding the path
os.path.relpath() returns a string, not an open file object. You'll need to open a file first; use the open() function:
def functION():
Source_obj = path.relpath(r"WebSource\EXAMPLE SOURCE.htm")
with open(Source_obj) as fileobj:
data = fileobj.read()
with here treats the file object as a context manager; when the indented codeblock under the statement is exited (either because the code completed or an exception occurred), the file object will automatically be closed.
Your Source_obj is just a string, not a file.
def functION():
Source_obj = path.relpath("WebSource\EXAMPLE SOURCE.htm")
with open(Source_obj) as f:
data = f.read()
By open()-ing it you can read from the file. Using the with context manager, the file will be properly closed for you when you leave that block of code.
I've got PyPDF2 lib from here:
https://github.com/mstamy2/PyPDF2/tree/Python3-3
When trying to run script "Example 1:" from from there see it:
PyPDF2 python versions (2.5 - 3.3) compatibility branch
Traceback (most recent call last):
File "1.py", line 6, in <module>
input1 = PdfFileReader(open("document1.pdf", "rb"))
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 595, in __init__
self.read(stream)
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 1097, in read
streamData = StringIO(xrefstream.getData())
TypeError: initial_value must be str or None, not bytes
What is wrong?
It was a problem related to the compatibility within PyPDF2 and Python 3.
In my case, I have solved it by replacing pdf.py and utils.py with the ones you will find here, where they basically control if you are running Python 3 and, in case you are, receive data as bytes instead of strings.
I would like to convert dozens of excel sheets to csv files at once. I have a working .vbs file which makes the conversion, and I would like to execute this .vbs file on the different sheets with the help of a python code. I have the following 2 versions of the python code:
Version 1:
import os
import sys
import subprocess
FolderName=sys.argv[1]
FileList=os.listdir(FolderName)
NewList=[]
for i in FileList:
NewItem=i.split('.xls')
NewXls=FolderName+"\\"+NewItem[0]+".xlsx "
NewCsv=FolderName+"\\"+NewItem[0]+".csv"
NewCommand="C:\\Users\\user\\XlsToCsv.vbs "+sys.argv[2]+" "+NewXls+NewCsv
subprocess.call(NewCommand)
Version 2:
import os
import sys
import subprocess
def main(directory,extension,sheet):
for filename in os.listdir(directory):
if filename.endswith(extension):
path = os.path.join(directory, filename)
base = os.path.join(directory, filename[:len(filename)-len(extension)])
print base
new_xls = base + extension
new_csv = base + '.csv'
subprocess.call(['C:\\Users\\user\\XlsToCsv.vbs', sheet, new_xls, new_csv])
main(sys.argv[1],sys.argv[2],sys.argv[3])
It does not matter, which I try, I get the same error message:
Traceback (most recent call last):
File "C:/Users/user/Desktop/Work/XlsDir.py", line 16, in <module>
subprocess.call(NewCommand)
File "C:\Python27\lib\subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 er ikke et gyldigt Win32-program
The last line of the error message means approximately, that it is not a valid Win32-program.
What I have tried so far:
If I run the .vbs file from command prompt with the right arguments (sheet, name of the .xls file and name of the .csv file) then it works fine.
If I print the commands that python generates and copy them into command prompt, they work fine.
I tried every combinations of '\' and '\' within the different paths, and nothing got any better.
I tried to execute the programs with replacing the sys.argv[i] arguments with specific arguments and then execute the .py file from command prompt. I get the same error message.
I hope some of you can help me. Thanks a lot!
To elaborate on Ansgar's remedy:
Starting a .vbs from the command line 'works', because the shell associates the extension .vbs with an application (e.g. cscript/wscript; see ftype, assoc, cscript //E, cescript //S).
subprocess.call() does not open a shell, so either specify the application (c|wscript.exe) or start the shell yourself:
import subprocess
#subprocess.call("notepad") # works
#subprocess.call("dir") # [Error 2] The system cannot find the file specified
# no shell, no intrinsics
#subprocess.call("19112944.vbs") # [Error 193] %1 is not a valid Win32 application
# no shell, can't associate .vbs with c|wscript.exe
subprocess.call("cscript 19112944.vbs") # works
subprocess.call("cmd /c 19112944.vbs") # works
# have shell, can associate .vbs with c|wscript.exe
Try running the script with cscript.exe:
subprocess.call(['cscript.exe', 'C:\\Users\\user\\XlsToCsv.vbs', sheet, new_xls, new_csv])