Python Forum
splitstring file names a by hyphen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitstring file names a by hyphen
#1
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")\
    
Reply
#2
filstring=(text.split("-")) is the same split of the input text as splitstring=(text.split("-"))
Reply
#3
(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.
Reply
#4
I figured it out, Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  rename same file names in different directories elnk 0 711 Nov-04-2022, 05:23 PM
Last Post: elnk
  Saving Excel workbook file with dataframe names Biplab1985 0 2,025 Jun-07-2020, 12:25 PM
Last Post: Biplab1985
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 5,690 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  Is there a more effecient way to do this ? File Names sumncguy 2 2,081 Jul-11-2019, 12:47 PM
Last Post: DeaD_EyE
  How to combine file names into a list from multiple directories? python_newbie09 3 5,206 Jul-09-2019, 07:38 PM
Last Post: python_newbie09
  write each line of a text file into separate text files and save with different names Shameendra 3 2,779 Feb-20-2019, 09:07 AM
Last Post: buran
  Reading file names CertifiedPengu 1 2,267 Jan-07-2019, 06:54 PM
Last Post: Gribouillis
  temporary file names Skaperen 6 3,728 Jan-02-2019, 07:14 PM
Last Post: Skaperen
  how to increment all file names in a folder SoulsKeeper 5 6,034 Sep-10-2018, 07:59 PM
Last Post: Skaperen
  dealing with spaces in file names AceScottie 5 75,091 Jun-02-2018, 01:06 PM
Last Post: AceScottie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020