Python Forum

Full Version: splitstring file names a by hyphen
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My script is supposed to go through a folder and search all the file names and split the text at the hyphen. My files are named, filename (hyphen) badge number, so example 20190101-B1234. I want to type in the badge number (numbers after the hyphen in file name).
So in this example I would have to search “B1234” and it should open folder “20190101-B1234”, but its not. Its opening the first folder in the directory every time.

import subprocess
import os, sys

text = input("Badge number: ")
splitstring=(text.split("-"))  
splitstring.pop(0) #split the text
paths = [ "\\\\Server\\case\\test\\" ]  #test folder
print("Searching ... \n")

found = 0
for i in range(0, len(paths)):
    path = paths[i]
    dirs = os.listdir(path)
    for fil in dirs:
        filstring=(text.split("-"))
        filstring.pop(0)

        if filstring == splitstring:
            print("Path to Case File: " + path + fil)
            openString = 'explorer "'+ path+ fil + '"'
            subprocess.Popen(openString)
            found = found + 1       
            break
if found == 0:
    print("Unable to find Case File for " + text)
    print("Check spelling and try agian.")
else:
    print("\n")\
    
filstring=(text.split("-")) is the same split of the input text as splitstring=(text.split("-"))
(Oct-23-2019, 08:43 PM)Yoriz Wrote: [ -> ]filstring=(text.split("-")) is the same split of the input text as splitstring=(text.split("-"))

I tried making them the same but it didn't work. I also tried taking each out and tested but that didn't work.
I figured it out, Thank you