I'm teaching myself Python thanks to a book and nice people i find every day in SO. Thanks all of you.
The question is that i been using VS, because i find it easy, but idk how/where to write commands like "python pythonprogram.py" and make use of the optparse module as i do on a Linux console.
The program i'm working on extracts the metadata from a pdf and it's the code below.
import PyPDF2
import optparse
def printMeta(fileName):
pdfFile = pdfFileReader(file(fileName, 'rb'))
docInfo = pdfFile.getDocumentInfo()
print('[*] PDF Metadata For: ' + str(fileName))
for metaItem in docInfo:
print('[+]' + metaItem + ':' + docInfo[metaItem])
def main():
parser = optparse.OptionParser('usage %prog' + '-F <PDF File Name>')
parser.add_option('-F', dest='fileName', type='string', help='specify PDF file Name')
(options, args) = parser.parse_args()
fileName = options.fileName
if fileName == None:
print(parser.usage)
exit(0)
else:
printMeta(fileName)
if __name__ == '__main__':
fileName = 'pdftest.pdf'
main()
This is a double question, one about VS proper use and the other to ask if my code works. I want to say that the pdftest.pdf is in the python program folder so i don't need a direction if i'm right. Thanks
To make sure where your relative path starts, you might want to use os.chdir
from os import chdir
chdir("path/to/my/python/file")
then, you'll be able to import files in the same directory even if you're running the script from another folder.
Hope that helps
Related
I have been given a Project on Python Programming so I wanted to ask you that how can I give relative directory paths to the generated files in Python so that it could be opened in other machines as absolute paths won't work on every PC
If you have write access to the folder containing your script then you can use something like
import sys, os
if __name__ == '__main__':
myself = sys.argv[0]
else:
myself = __file__
myself = os.path.abspath(myself)
whereami = os.path.dirname(myself)
print(myself)
print(whereami)
datadir = os.path.join(whereami, 'data')
if not os.path.exists(datadir):
os.mkdir(datadir)
datafile = os.path.join(datadir, 'foo.txt')
with open(datafile, 'w') as f:
f.write('Hello, World!\n')
with open(datafile) as f:
print(f.read())
In the file that has the script, you want to do something like this:
import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'relative/path/to/file/you/want')
This will give you the absolute path to the file you're looking for, irrespective of the machine you are running your code on.
You can also refer these links for more information:
Link1
Link2
For more specific information, please make your question specific. i.e Please post the code that you have tried along with your inputs and expected outputs.
couple of days back an infected computer infected my USB drive with a shortcut virus and. I had lots of software in that USB drive i plugged it in my Linux machine and cleaned a lot of files but what the virus did is it created an .exe file in each folder with that folder's name. Each .exe file is capable of infecting another PC. So me with a lot of time in my hand was trying to make a python script that goes to each folder check if a .exe file with that folder name exists delete it and run the function for every sub-folder recursively. but it is not working here what i made so far
#!/usr/bin/python
from __future__ import print_function
import os
import argparse
def deldir(fol):
# changing to the directory
os.chdir(fol)
#Trivial case delte the virus in the root folder
cwd = os.getcwd()
#getting just the name of the folder rather than full path by splitting from "/"
cwd = str(cwd).split('/')[-1]
#if any .exe file exists with that name
if any(str(cwd + '.exe') in s for s in os.listdir('.')):
# delete that file
os.remove(str(cwd + '.exe'))
print('Deleting virus file')
#making a list with all the contents in the directory
dirlist = os.listdir('.')
print(dirlist)
#Looping through the directory that we just made a list of its contents
for i in dirlist:
#checking if it is a directory or not
if os.path.isdir(i):
#changing dir to that directory
os.chdir(i)
#getting the current working directory
cwd = os.getcwd()
#getting just the name of the folder rather than full path by splitting from "/"
cwd = str(cwd).split('/')[-1]
#if any .exe file exists with that name
if any(str(cwd + '.exe') in s for s in os.listdir('.')):
# delete that file
os.remove(str(cwd + '.exe'))
print('Deleting virus file', str(cwd + '.exe'))
#listing filders in current directory
for j in os.listdir('.'):
#passing that subdirectory to the function itself if its a folder
if os.path.isdir(j):
deldir(j)
else:
print('Not a folder so skipping')
def main():
#parsing the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("fol", help="Directory to enter")
args = parser.parse_args()
#passing the command line argument with the folder path to "deldir" function
deldir(args.fol)
if __name__ == "__main__":
main()
Please point me to right direction i have no idea what i am doing wrong. Thanks for reading
Here what I see mistake in your code.
1
os.getcwd -> os.getcwd()
It's a function
2.
You using os.chdir(i) inside the for loop. this will work only first directory in that directory.
3.
The algorithm is wrong, It's not checking the directories level 1,3,5,... for virus.
As Hiro and MYGz's comments. os.walk/os.scandir is easier way to go for recursive directory. but your way is better for practice and learn programming.
I got it done the way i wanted with os.walk() it's really easy that way and that takes away the fun ! :'( . I was hoping to fix my own code but somethings not right. well anyways here's the working code if anyone encounters this type of virus or something.
#!/usr/bin/python
from __future__ import print_function
import os
import argparse
def deldir(fol):
for root, dirs, files in os.walk(fol):
try:
os.remove(str(root + '/' + str(root).split('/')[-1 ] + '.exe'))
except:
print('Does not exists' ,str(root + '/' + str(root).split('/')[-1 ] + '.exe'))
def main():
#parsing the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("fol", help="Directory to enter")
args = parser.parse_args()
#passing the command line argument with the folder path to "deldir" function
deldir(args.fol)
if __name__ == "__main__":
main()
I'm trying to read arguments with spaces in windows cmd.
So here is the code.
from avl_tree import *
import sys,os
if __name__ == '__main__':
avl = AVLTreeMap()
infile = sys.argv[1] + '.txt'
avl._preprocessing(infile)
avl._interface(infile)
I've written it as sys.argv[1] since I'm gonna type in the cmd as following:
python filename.py textfilename
But then if the text file has spaces in the name it won't work like that.
Any suggestions?
Thanks in advance.
This is a very hacky fix, and I wouldn't necessarily suggest it because it will mess with other arguments you might need to add later but you could do something like this:
infile = " ".join(sys.argv[1:]) + '.txt'
So you if you run the program like this:
python filename.py my file name
infile will equal "my file name.txt"
Please bear with me, I've not used python before, and I'm trying to get some rendering done as quick as possible and getting stopped in my tracks with this.
I'm outputting the .ifd files to a network drive (Z:), and they are stored in a folder structure like;
Z:
- \0001
- \0002
- \0003
I need to iterate over the ifd files within a single folder, but the number of files is not static so there also needs to be a definable range (1-300, 1-2500, etc). The script therefore has to be able to take an additional two arguments for a start and end range.
On each iteration it executes something called 'mantra' using this statement;
mantra -f file.FRAMENUMBER.ifd outputFile.FRAMENUMBER.png
I've found a script on the internet that is supposed to do something similar;
import sys, os
#import command line args
args = sys.argv
# get args as string
szEndRange = args.pop()
szStartRange = args.pop()
#convert args to int
nStartRange = int(szStartRange, 10);
nEndRange = int(szEndRange, 10);
nOrd = len(szStartRange);
#generate ID range
arVals = range(nStartRange, nEndRange+1);
for nID in arVals:
szFormat = 'mantra -V a -f testDebris.%%(id)0%(nOrd)dd.ifd' % {"nOrd": nOrd};
line = szFormat % {"id": nID};
os.system(line);
The problem I'm having is that I can't get it to work. It seems to iterate, and do something - but it looks like it's just spitting out ifds into a different folder somewhere.
TLDR;
I need a script which will at least take two arguments;
startFrame
endFrame
and from those create a frameRange, which is then used to iterate over all ifd files executing the following command;
mantra -f fileName.currentframe.ifd fileName.currentFrame.png
If I were able to specify the filename and the files directory and output directory that'd be great too. I've tried manually doing that but there must be some convention to that I don't know as it was coming up with errors when I tried (stopping at the colon).
If anyone could hook me up or point me in the right direction that'd be swell. I know I should try and learn python, but I'm at my wits end with the rendering and need a helping hand.
import os, subprocess, sys
if len(sys.argv) != 3:
print('Must have 2 arguments!')
print('Correct usage is "python answer.py input_dir output_dir" ')
exit()
input_dir = sys.argv[1]
output_dir = sys.argv[2]
input_file_extension = '.txt'
cmd = 'currentframe'
# iterate over the contents of the directory
for f in os.listdir(input_dir):
# index of last period in string
fi = f.rfind('.')
# separate filename from extension
file_name = f[:fi]
file_ext = f[fi:]
# create args
input_str = '%s.%s.ifd' % (os.path.join(input_dir, file_name), cmd)
output_str = '%s.%s.png' % (os.path.join(output_dir + file_name), cmd)
cli_args = ['mantra', '-f', input_str, output_str]
#call function
if subprocess.call(cli_args, shell=True):
print('An error has occurred with command "%s"' % ' '.join(cli_args))
This should be sufficient for you to either use currently or with slight modification.
Instead of specifically inputting a start and end range you could just do:
import os
path, dirs, files = os.walk("/Your/Path/Here").next()
nEndRange = len(files)
#generate ID range
arVals = range(1, nEndRange+1);
The command os.walk() counts the # of files in the folder that you specified.
Although, an even easier way of getting your desired output is like this:
import os
for filename in os.listdir('dirname'):
szFormat = 'mantra -f ' + filename + ' outputFile.FRAMENUMBER.png'
line = szFormat % {"id": filename}; # you might need to play around with this formatting
os.system(line);
Because os.listdir() iterates through the specified directory and filename is every file in that directory, so you don't even need to count them.
a little help building the command.
for nID in arVals:
command = 'mantra -V a -f '
infile = '{0}.{1:04d}.ifd '.format(filename, id)
outfile = '{0}.{1:04d}.png '.format(filename, id)
os.system(command + infile + outfile);
and definitely use os.walk or os.listdir like #logic recommends
for file in os.listdir("Z:"):
filebase = os.path.splitext(file)[0]
command = 'mantra -V a -f {0}.ifd {0}.png'.format(filebase)
Can anyone guide me how can I get file path if we pass file from command line argument and extract file also. In case we also need to check if the file exist into particular directory
python.py /home/abhishek/test.txt
get file path and check test.txt exist into abhishek folder.
I know it may be very easy but I am bit new to pytho
import os
import sys
fn = sys.argv[1]
if os.path.exists(fn):
print os.path.basename(fn)
# file exists
Starting with python 3.4 you can use argparse together with pathlib:
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("file_path", type=Path)
p = parser.parse_args()
print(p.file_path, type(p.file_path), p.file_path.exists())
I think the most elegant way is to use the ArgumentParser This way you even get the -h option that helps the user to figure out how to pass the arguments. I have also included an optional argument (--outputDirectory).
Now you can simply execute with python3 test.py /home/test.txt --outputDirectory /home/testDir/
import argparse
import sys
import os
def create_arg_parser():
# Creates and returns the ArgumentParser object
parser = argparse.ArgumentParser(description='Description of your app.')
parser.add_argument('inputDirectory',
help='Path to the input directory.')
parser.add_argument('--outputDirectory',
help='Path to the output that contains the resumes.')
return parser
if __name__ == "__main__":
arg_parser = create_arg_parser()
parsed_args = arg_parser.parse_args(sys.argv[1:])
if os.path.exists(parsed_args.inputDirectory):
print("File exist")
Use this:
import sys
import os
path = sys.argv[1]
# Check if path exits
if os.path.exists(path):
print "File exist"
# Get filename
print "filename : " + path.split("/")[-1]