0

I have a script that looks into a .txt file like this:

house.txt:
1289
534
9057
12873

(every line is meant to be a "CODE" for a product)

and it looks for a filename with that code in a given folder and copies it to another folder.

Everything works fine, except if this happens:

0001_filename_blablalba.jpg
00011 filename.jpg
000123Filename.jpg

I want to copy the file with the string "0001" but the script copies all the above because indeed they have 0001, but it's not the whole code.

Here's my script:

import subprocess
with open('CASA.txt','r') as f:
lines = [line.rstrip('\n') for line in f]

for ID in lines:
id_produto = str(ID+'*')
command = "find . -maxdepth 1 -name '%s' -exec ditto -v {} ./imagenss/ \;"%id_produto
print "A copiar: %s"%id_produto
proc = subprocess.Popen(command,shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Is there a simple way to do this?

2
  • this question not about python, it is about bash Commented Nov 23, 2015 at 12:15
  • Please edit your title to be more descriptive of your problem. As written, it is too broad. Commented Dec 2, 2015 at 3:05

3 Answers 3

1

You are somewhat mixing Python and shellsripting - but still you could try another filename pattern:

Instead of

id_produto = str(ID+'*')

try

id_produto = str(ID+'[!0-9]*')

This will match anything that starts with the ID followed by anything else but a number. If you want to do a pythonic way, use the package glob for filename matching and os for copying ...

Sign up to request clarification or add additional context in comments.

1 Comment

THIS WAS IT!!! omg thank you so much you saved my lifework so simple but so effective
0

You can try to use the "split" function to split your filename string. You'll have to analyze the remaining part of the string and see if there are digits left (i.e. the ID only match a part of the complete ID of the file) or if there are no digits left (i.e. you found the full ID so you can copy the file):

completeFilename = '12345_filename.jpg'
ID = '123'

fileName = completeFilename.split(ID)[1]

if fileName[0].isdigit():
    #There are some digit left, so this file should not be copied
else:
    #No digits left, copy this file

2 Comments

Hello! thanks for your answer, i thought about that but I have filenames that do indeed have digits in the filename like "123_wine20years.jpg Is there another way around this? Thanks again
What the piece of code above does is to look at the first character in the string left after removing the ID. So in case your file name is "123_wine20years.jpg" the code will find a "_" character after removing the '123' code, so it will copy it. I guess that's the expected behavior.
0

It's better to use python code instead of shell command to find the file.

import os

def get_base(filename):
    'get the "code" for a filename'
    out=''
    for char in filename:
        if char.isdigit():
            out+=char
        else:
            return out

with open('path/to/the/txt_file.txt','r') as f:
    lines=f.splitlines()

files=os.listdir('path/to/the/folder')
files_dict={get_base(x):x for x in files}

for line in lines:
    print('copy %s'%files_dict.get(line,None))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.