Kortike
Kortike

Reputation: 37

Python 2.7 Search directory based on user input and open all files in folder

I have been searching high and low for a way to piece this together to no avail. I am pretty rusty with my python but am trying to write a (what I consider) simple program and I am nearly bald. The goal of this program is for someone to enter a Part No as input i.e. "ABC" and to search a directory for a folder matching the name and then to open all files within the folder. This would generally be a .pdf and possibly 2-3 excel files. I was able to make this work while testing by using open() with the entire file path(s) pasted in but I want it to be based upon user input. When I run the code below I see that it found the file in the correct directory (I made a file named ABC.txt for testing) but would not open it because it had added an extra .txt extension to the filename.

import os
import os.path
partno =""
partno = raw_input("Enter Part No:") #input from user to search dir
yourpath = r'C:\Python27' 
#All folders to be searched stem from one parent dir
print ("Loading Part No %r files...Please wait") % partno
for dname, _, fnames in os.walk(r'C:\Python27'):
    for fname in fnames:
        if partno in fname:
            filepath = (os.path.abspath(os.path.join(dname, fname)))
open (filepath,"r")
print filepath

Here is the output I receive...but it will not open the file.

Enter Part No:ABC
Loading Part No 'ABC' files...Please wait
C:\Python27\Paperless Test\ABC\ABC.txt.txt

Edit: Sorry for the missing line I am away from my PC so I am using remote desktop and forgot to update this after I disconnected.

Updated and finished code today. Works perfectly now minus one small issue which isn't a major issue and should be pretty easily resolved.

Here is the updated code for my actual directory. Thanks again for all the help guys!

import os

partno =""
partno = raw_input("Enter Part No:") #Takes user input of Part No
yourpath = r"Y:\SHOP\MANUFACTURING DOCS" #Parent directory
print ("Loading Part No %r files...Please wait") % partno

for root, dirs, files in os.walk(yourpath, topdown=True):
    for name in dirs:
        if name == partno: #Matches folder in directory to `part no`
            path_to_files = (os.path.abspath(os.path.join(root, name)))
            #Stores path in variable so files are only found in this folder
            print path_to_files #Confirmation line for me 
for dname, _, fnames in os.walk(path_to_files, topdown=True):
    for fname in fnames:
        if fname.startswith(partno): 
            filepath = (os.path.abspath(os.path.join(dname, fname)))
            os.startfile(filepath) #Opens all files in folder in their 
            native applications
            print filepath
raise SystemExit

Note: When I run this it finds and prints the directory extremely fast but when it runs the 2nd half of the program it takes 5-10 seconds to find, print and open the files. Does this have something to do with the way I have it coded?

Upvotes: 0

Views: 1247

Answers (1)

SwiftsNamesake
SwiftsNamesake

Reputation: 1578

As stated in the comments, open doesn't actually open files in the way you're thinking about it, it simply hands you a reference to the file in question.

To launch an application with the given file (on Windows), you need a different function:

os.startfile(filename)

The accepted answer to the question @identicon links to mentions a different approach, which happens to be cross-platform (using os.subprocess).

Upvotes: 0

Related Questions