-1

I have an Excel file that contains long product tag name like(for now, just working on 3 of them):

  1. 4049047000037
  2. 4049047000044
  3. 4049047118954

and i have a folder on my desktop called "1" containing .jpg files with tag names like:

  1. 4049047000037.jpg
  2. 4049047000044.jpg
  3. 4049047118954.jpg

i want to write a code, if tag name in my excel, i want to copy that .jpg file to an other folder.

import os
import pandas as pd
movdir = ["C:\Users\muhammedcan\Desktop\1"]
basedir = "C:\Users\muhammedcan\Desktop\2"
i=0

#to see what i have in my folder
print os.listdir("C:/Users/muhammedcan/Desktop/1/")

df= pd.read_excel("deneme.xls", sheetname= "sayfa4")
df1= df.columns[1]

listSepalWidth = df[df1]
print listSepalWidth

#to make file name and product tag name same
for i in listSepalWidth:
    i=str(i)+(".jpg")
    print i

can you help me with copying file into an other file if it is exist in my excel?

this is my result so far:

['4049047000037.jpg', '4049047000044.jpg', '4049047000068.jpg', 
'4049047000075.jpg', '4049047000082.jpg', '4049047000105.jpg', 
'4049047118947.jpg', '4049047118954.jpg']

4049047000037.jpg
4049047000044.jpg
4049047118954.jpg
4049047000068.jpg
4049047000075.jpg
4049047000082.jpg
4049047118947.jpg
4049047000105.jpg

I used following code, and I am recieving error.

from shutil import copyfile

copyfile("C:\Users\muhammedcan\Desktop\1", "C:\Users\muhammedcan\Desktop\2")

Error is:

    C:\Python27\python.exe "C:/Users/muhammedcan/Desktop/summer 
    courses/programing/MP4/solution/my_work.py"
    Traceback (most recent call last):
  File "C:/Users/muhammedcan/Desktop/summer courses/programing/MP4/solution/my_work.py", line 3, in <module>
    copyfile("C:\Users\muhammedcan\Desktop\1", 

Process finished with exit code 1

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
H. Can
  • 19
  • 7
  • 1
    Possible duplicate of [How do I copy a file in python?](https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) – DeepSpace Feb 28 '18 at 15:08
  • I am new to python, I cannot understand "src","dst" things can you help me with code? @DeepSpace – H. Can Feb 28 '18 at 15:12
  • Did you even *try* to understand? It is perfectly explained in the answer of the duplicated question: `Copy the contents of the file named src to a file named dst` – DeepSpace Feb 28 '18 at 15:17
  • I have been trying to figure out isue for 3 day.. it is easy to judge people. tnx any way. @DeepSpace – H. Can Feb 28 '18 at 15:49
  • What part of "Copy the contents of the file named src to a file named dst" is not clear? – DeepSpace Feb 28 '18 at 15:51
  • writing src is enough? how to define src? – H. Can Feb 28 '18 at 15:55
  • `src` should be the path of the file you'd like to copy, `dst` is the destination path. You can't just write `src`. How will Python know which file you want to copy, and to where? – DeepSpace Feb 28 '18 at 15:57
  • but i need some files in the folder not all. @DeepSpace – H. Can Feb 28 '18 at 16:25

1 Answers1

1

The following should do what you are looking for:

import os
import glob
import pandas as pd
import shutil

source_folder = r"C:\Users\muhammedcan\Desktop\1"
destination_folder = r"C:\Users\muhammedcan\Desktop\2"

available_filenames = [os.path.basename(fn) for fn in glob.glob(os.path.join(source_folder, '*.jpg'))]
df = pd.read_excel("deneme.xls", sheetname="sayfa4")

for tag_name in df.iloc[:,1]:
    filename = "{}.jpg".format(tag_name)

    if filename in available_filenames:
        print "{} - found".format(filename)
        shutil.copyfile(os.path.join(source_folder, filename), os.path.join(destination_folder, filename))
    else:
        print "{} - not found".format(filename)

If first creates a list of .jpg filenames found in the source_folder. It then loads the Excel file into pandas and iterates over the second column. If the tag name is found in the list of available_filenames the shutil.copyfile() function is used to copy the file from 1 to 2. Note os.path.join() is used to safely join parts of a file together.


To make it into a function to let you also do 'pdf' you could do:

import os
import glob
import pandas as pd
import shutil

source_folder = r"C:\Users\muhammedcan\Desktop\1"
destination_folder = r"C:\Users\muhammedcan\Desktop\2"

df = pd.read_excel("deneme.xls", sheetname="sayfa4")


def copy_files(source_folder, destination_folder, extension):
    available_filenames = [os.path.basename(fn) for fn in glob.glob(os.path.join(source_folder, '*.{}'.format(extension)))]

    for tag_name in df.iloc[:,1]:
        filename = "{}.{}".format(tag_name, extension)

        if filename in available_filenames:
            print "{} - found".format(filename)
            shutil.copyfile(os.path.join(source_folder, filename), os.path.join(destination_folder, filename))
        else:
            print "{} - not found".format(filename)

copy_files(source_folder, destination_folder, 'jpg')            
copy_files(source_folder, destination_folder, 'pdf')                   

This assumes the same deneme.xls is used for both. If not it could be passed as another argument to the function.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Thank you @Martin Evans ! Can I apply same for pdf files? – H. Can Mar 01 '18 at 09:55
  • It would be easiest to make this a function and pass the extension you want as a parameter. – Martin Evans Mar 01 '18 at 09:56
  • I am thankful @Martin . I have an other isue now. In some cells I have data as follows: 456.jpg,457.jpg I do not know how to overcome this. What I want from my code is to find both 456jpg and 457.jpg files. – H. Can Mar 02 '18 at 16:14