473,763 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program that can find a find a file for you ?

Greetings.

Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I should
be able to write in the command line:
python name of my program / the libary to search and what kind of file it
is example a .pdf file
So if my program name was test.py and the library name was library1 and the
test type i wanted to find was, a .pdf file
I should write python test.py /library1 .pdf

I have to use some of the code below, but im very lost, I know that I have
use os, but not quite sure. Does any have any ideas.....

Thanks all

***
# -*- coding: utf-8 -*-
import sys

def useArgument(arg ):
if __name__=="__ma in__":
"""
python argtest.py arg1 arg2 arg3
"""
if len(sys.argv)!= 4:
sys.exit("Error , not the right amout of arguments")

for arg in sys.argv:
useArgument(arg )

***
Jul 18 '05 #1
6 1779
Peter Hansen wrote:
Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I should
be able to write in the command line:
python name of my program / the libary to search and what kind of file it
is example a .pdf file
So if my program name was test.py and the library name was library1 and the
test type i wanted to find was, a .pdf file
I should write python test.py /library1 .pdf

I have to use some of the code below, but im very lost, I know that I have
use os, but not quite sure. Does any have any ideas.....


You should investigate using the standard library module
getopt, or perhaps optparse (a newer one), as they are
designed to handle command line arguments with ease. Doing
it yourself is probably a waste of time.

(By the way, would you consider changing your "From:"
and "Reply-To:" headers to include an initial or something?

Having two Peter Hansens around here could be confusing,
and I've been a frequent contributor so you might find
many people thinking you're me and sending you hate mail
or something, and you wouldn't know why... If you don't
want to do that, I will. I just hope your middle name
doesn't begin with "L"...)

-Peter L Hansen (the regular)
Jul 18 '05 #2
Am Wed, 29 Sep 2004 11:25:39 +0200 schrieb Peter Hansen:
Greetings.

Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I should
be able to write in the command line:
python name of my program / the libary to search and what kind of file it
is example a .pdf file
So if my program name was test.py and the library name was library1 and the
test type i wanted to find was, a .pdf file
I should write python test.py /library1 .pdf


Hi,

This is something the "find" command does in a unix environment.

Here is my solution:

You could use this:
find.py your_path 'library1.*\.pd f$'

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# Python Imports
import os
import re
import sys

def usage():
print """Usage: %s path regex
Print all files or directories with match the regex.

See this URL for the syntax of the regular expressions
http://docs.python.org/lib/re-syntax.html

""" % (os.path.basena me(sys.argv[0]))

def visit(regex, dirname, names):
for name in names:
p=os.path.join( dirname, name)
if regex.search(p) :
print p
def main():
if len(sys.argv)!= 3:
usage()
sys.exit(1)
path=sys.argv[1]
regex=sys.argv[2]
os.chdir(path)
regex=re.compil e(regex)
os.path.walk(". ", visit, regex)

if __name__=="__ma in__":
main()

Jul 18 '05 #3
Hi again all.
I allmost did it, just need the line to run the program now, any ideas, my
head hurts, cant think anymore..... Thanks for your help

import sys
import os.path
import os.dir
a = sys.argv[2]
b = sys.argv[3]

def func (bib, end):
c = os.path.dirlist (bib)

for x in c:
d = os.dir.split(x)
if [1] = end
print (bib, end)

if __name__=="__ma in__":

"Thomas Guettler" <gu*****@thom as-guettler.de> wrote in message
news:pa******** *************** *****@thomas-guettler.de...
Am Wed, 29 Sep 2004 11:25:39 +0200 schrieb Peter Hansen:
Greetings.

Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I
should
be able to write in the command line:
python name of my program / the libary to search and what kind of file
it
is example a .pdf file
So if my program name was test.py and the library name was library1 and
the
test type i wanted to find was, a .pdf file
I should write python test.py /library1 .pdf


Hi,

This is something the "find" command does in a unix environment.

Here is my solution:

You could use this:
find.py your_path 'library1.*\.pd f$'

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# Python Imports
import os
import re
import sys

def usage():
print """Usage: %s path regex
Print all files or directories with match the regex.

See this URL for the syntax of the regular expressions
http://docs.python.org/lib/re-syntax.html

""" % (os.path.basena me(sys.argv[0]))

def visit(regex, dirname, names):
for name in names:
p=os.path.join( dirname, name)
if regex.search(p) :
print p
def main():
if len(sys.argv)!= 3:
usage()
sys.exit(1)
path=sys.argv[1]
regex=sys.argv[2]
os.chdir(path)
regex=re.compil e(regex)
os.path.walk(". ", visit, regex)

if __name__=="__ma in__":
main()

Jul 18 '05 #4
You didn't almost do it, you are still very far from it. The code you've
written so far is full of mistakes and I'm really not sure even what you are
trying to do.

Instead of writing the whole program in one shot, try to write just
something very small, try it, make it work and then add one more small
thing. For instance, try to run a script with only the first 5 lines that
you wrote and you will find a problem already. Fix that and then add the
function with only its first statement. Invoke the function and you will
find another problem. Fix that.

And so on.

I'm curious though. Is this maybe an assignment for a course that you are
taking?

Dan

"Peter..... " <he********@yah oo.com> wrote in message
news:41******** *************** @nntp03.dk.teli a.net...
Hi again all.
I allmost did it, just need the line to run the program now, any ideas, my
head hurts, cant think anymore..... Thanks for your help

import sys
import os.path
import os.dir
a = sys.argv[2]
b = sys.argv[3]

def func (bib, end):
c = os.path.dirlist (bib)

for x in c:
d = os.dir.split(x)
if [1] = end
print (bib, end)

if __name__=="__ma in__":

"Thomas Guettler" <gu*****@thom as-guettler.de> wrote in message
news:pa******** *************** *****@thomas-guettler.de...
Am Wed, 29 Sep 2004 11:25:39 +0200 schrieb Peter Hansen:
Greetings.

Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I
should
be able to write in the command line:
python name of my program / the libary to search and what kind of file
it
is example a .pdf file
So if my program name was test.py and the library name was library1 and
the
test type i wanted to find was, a .pdf file
I should write python test.py /library1 .pdf


Hi,

This is something the "find" command does in a unix environment.

Here is my solution:

You could use this:
find.py your_path 'library1.*\.pd f$'

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# Python Imports
import os
import re
import sys

def usage():
print """Usage: %s path regex
Print all files or directories with match the regex.

See this URL for the syntax of the regular expressions
http://docs.python.org/lib/re-syntax.html

""" % (os.path.basena me(sys.argv[0]))

def visit(regex, dirname, names):
for name in names:
p=os.path.join( dirname, name)
if regex.search(p) :
print p
def main():
if len(sys.argv)!= 3:
usage()
sys.exit(1)
path=sys.argv[1]
regex=sys.argv[2]
os.chdir(path)
regex=re.compil e(regex)
os.path.walk(". ", visit, regex)

if __name__=="__ma in__":
main()


Jul 18 '05 #5
Hi Peter,

On Wed, 29 Sep 2004 11:25:39 +0200, Peter Hansen <he********@yah oo.com> wrote:
Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I should
be able to write in the command line:
python name of my program / the libary to search and what kind of file it
is example a .pdf file


I had to do something like this sometime back so I wrote up a general
purpose script that would look for certain types of files and call a
python function, passing the filename as argument to the function.
This function could be any thing that you would care to define (My
script incidentally just rename file with *ill formed* names). This is
roughly the equivalent doing this using the 'find' unix command:

$ find -name "*.ext" -exec (some python function) {} ';'

You can find the function at the ASPN cookbook site:

http://aspn.activestate.com/ASPN/Coo.../Recipe/300411

Hope you find it useful.

Regards
Steve
Jul 18 '05 #6
Thanks for your input.

I solved the last few lines and corrected the errors that was in it.
And it does work.
Stay happy.....

"Steve" <lo******@gmail .com> wrote in message
news:ma******** *************** *************** @python.org...
Hi Peter,

On Wed, 29 Sep 2004 11:25:39 +0200, Peter Hansen <he********@yah oo.com>
wrote:
Im trying to write a program that can be run from the command line.
If I want to search for example after a file with the ending .pdf, I
should
be able to write in the command line:
python name of my program / the libary to search and what kind of file
it
is example a .pdf file


I had to do something like this sometime back so I wrote up a general
purpose script that would look for certain types of files and call a
python function, passing the filename as argument to the function.
This function could be any thing that you would care to define (My
script incidentally just rename file with *ill formed* names). This is
roughly the equivalent doing this using the 'find' unix command:

$ find -name "*.ext" -exec (some python function) {} ';'

You can find the function at the ASPN cookbook site:

http://aspn.activestate.com/ASPN/Coo.../Recipe/300411

Hope you find it useful.

Regards
Steve

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2075
by: Bob | last post by:
I need to create a program that is essentially a special fax sender using multi line Dialogic cards. I figure that the best way to do this so that it can be used from any app is to create someting that can be installed as a printer on the local computer. When sending the print command so that the current document, whatever it is is transmitted to the program, I need to popup a box to get the desination info (name phone number etc..) ,when...
0
2424
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical price of a stock. The program will allow users to query the stock price of a particular date and output its statistics (mean price, median price and standard deviation) for any specified period. Your program must be menu driven and works as follows.
4
2759
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last time i ask for an entire code or u can give me the outine of what to do and i ll do it by myself. the description is the following: the program will read a text file that contains historical price of a stock. The program will allow users to query...
1
3996
by: amir | last post by:
Hello all, I have a db file .mdf in my program. I wander if it saves it everytime values are inserted and if not how can it be done ? furthermore i want to know where can i see the .mdf file after the program closes and with what program. cheers, amir.
11
7698
by: hamiltongreg | last post by:
I am new to Java and am having problems getting my program to compile correctly. My assignment is as follows; Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software). • Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit. • Create a Java application that displays the...
1
6020
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main function which i think has problem // this a simple program about phonebook,it can add items,del items,find items and #include "member.h" #include <fstream> #include <vector>
10
1997
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition which I can later copy and past into a python program. I will eventually expand the program to also output an SQL script to create a SQL file in MySQL The program still need a little work, it does not handle the following items
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.