Avatar of peetm
peetmFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Whereis in Python

I'd be interested to see a whereis type program written in Python.  Whereis - as in - whereis 'this' file along my PC's path.

I've written various version in Ruby, PHP C, Java etc, and would like to compare those against a Python implmentation [as well as to other languages].

Anyone fancy starting me off?
Python

Avatar of undefined
Last Comment
pepr
Avatar of ramrom
ramrom
Flag of United States of America image

Take a look at the walk function in the os module.
Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Ok, thanks - but let me expand ... I don't know anything about Python.  So, 'walk' functions in Python won't probably help me here - in that I'd not know how to use it [function/module].

I'm just trying to compare languages - for my own interests sake.

As I've said, I've solicited/written a few whereis programs in a variety of languages, but haven't got one in Python - so am curious to see how readable/concise such a program might be.

This is *just* for curiosities' sake -- see www.peetm.com/blog for the past 'efforts'!
Avatar of ramrom
ramrom
Flag of United States of America image


# Here is the minimal version. 
import os
for root, dirs, files in os.walk('c:\\program files\\python25'):
  if 'this.txt' in files:
    print root
 
# If you want to pass the file name via the command line 
import os, sys
for root, dirs, files in os.walk('c:\\program files\\python25'):
  if sys.argv[1] in files:
    print root

Open in new window

Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

@ramrom

Could you tell me how to run this in a console?

Pleas also see:

https://www.experts-exchange.com/questions/23582539/Whereis-in-Perl.html?anchorAnswerId=22054564#a22054564
Avatar of ramrom
ramrom
Flag of United States of America image

Depends (in part) on your OS

In general:

prompt>path-to-python/python.exe path-to-program/program.py this.txt
Avatar of pepr
pepr

peetm, you may want to use the following code for determining the full name of the program. When saved to a.py on my computer, the output looks like this:

C:\tmp\___python\t>a.py
C:\tmp\___python\t\a.py
C:\tmp\___python\t\a.py

C:\tmp\___python\t>

Note, the first line shows launching the script. The next two lines are the results of the print commands.
import os     # import the os module only if you find it necessary
import sys    # sys.argv contains command line arguments and also the name of the script
 
pgm = sys.argv[0]
print pgm                   # not neccessarily the full path on some systems
print os.path.abspath(pgm)  # this is absolutized and normalized

Open in new window

Avatar of ramrom
ramrom
Flag of United States of America image

I just re-read your question and noticed you said "along my PC's path". I had misread that as "along my Python path."

You'd have to examine each drive. There are ways to get a list of drives but I don't recall how one does that.
import os, sys
for drive in "CDE": # for example
  for root, dirs, files in os.walk(drive + ":\\"):
    if sys.argv[1] in files:
      print root

Open in new window

Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

@ramrom

Actually, I mean path as in the PATH environment variable.
ASKER CERTIFIED SOLUTION
Avatar of ramrom
ramrom
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Many thanks!
Avatar of pepr
pepr

Well, just for your information. This kind of utility is was always called "which" (in UNIX based systems). The name probably came from "WHICH programm will be called when I type PGM on command line" -- i.e. "which PGM".
Python
Python

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in other languages. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive set of standard libraries, including NumPy, SciPy, Django, PyQuery, and PyLibrary.

6K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo