0

@nmnhI'm trying to move over 200 pdf files, each to separate folders that are already created and named 2018. The destination path for each is like- GFG-0777>>2018. Each pdf has an unique GFG-0### name that matches the folders I already created that lead to the 2018 destination folders. Not sure how to iterate and get each pdf into the right folder.... :/

I've tried shutil.move which i think is best but have issues with paths I think.

import os
import shutil


srcDir = r'C:\Complete'
#print (srcDir)
dstDir = r'C:\Python27\end_dir'
dirList = os.listdir(srcDir)
for f in dirList:
    fp = [f for f in dirList if ".pdf" in f] #list comprehension to iterate task (flat for loop)
for file in fp:
    dst = (srcDir+"/"+file[:-4]+"/"+dstDir+"/"+"2018")
    shutil.move(os.path.join(srcDir, dst, dstDir))         

error: shutil.move(os.path.join(srcDir, dst, dstDir)) TypeError: move() missing 1 required positional argument: 'dst'

SJCC
  • 1
  • 1
  • What did you try and why did it not work? Can you post your code/trace? But essentially you need to get a list of your pdf names. E. G. Os. Listdir() and loop through them given the name and path – nmanh Apr 09 '19 at 23:11
  • Thanks for responding nmanh. I'll respond a bit later with details when get into work. Basically imported os and shutil. Tried to os.path.join the dst and src directories. I also use a comprehension list for loop calling the os.listdir – SJCC Apr 10 '19 at 12:30
  • You could just update your original answer, would be better because of formatting :) – nmanh Apr 10 '19 at 13:41
  • @nmanh ok.. obviously new to this – SJCC Apr 10 '19 at 13:43
  • @nmnh fixed format of code. – SJCC Apr 10 '19 at 15:04
  • @nmanh. Hey. Yeah. I answered myself. see below. Thanks for assist! – SJCC Apr 14 '19 at 21:41
  • If my answer helped you, it would be great if you can mark it to be {a/the} answer. Happy to help :) – nmanh Apr 15 '19 at 10:36

2 Answers2

0

AFAICT you are calling shutil.move(os.path.join(srcDir, dst, dstDir)) without a to. According to the documentation you need to have a from and to folder. https://docs.python.org/3/library/shutil.html#shutil.move

I guess your idea was to somehow create a string containing the dst and src :

dst = (srcDir+"/"+file[:-4]+"/"+dstDir+"/"+"2018")

What you actually want is something along this line:

dst_dir = dstDir+"/"+"2018"
src_dir = srcDir+"/"+file[:-4]
shutil.move(src_dir,dst_dir)

Above code is just for demonstration. If this does not work you could tree or ls -la example a small part of your srcdir and dstdir and we could work something out.

nmanh
  • 325
  • 2
  • 14
0

@nmanh I managed to work it out. Thanks for calling out the issue to create string with src and dst. After removing the string, I tweaked a bit more but found I had too many "file" in code. I had to make two of them "file1" and add a comma in the shutil.move between src and dst. Thanks again

import os
import shutil


srcDir = r'C:\Complete'
#print (srcDir)
dstDir = r'C:\Python27\end_dir'
dirList = os.listdir(srcDir)
for file in dirList:
    fp = [f for f in dirList if ".pdf" in f] #list comprehension to iterate task 
    (flat for loop)
for file in fp:
    if ' ' in file: #removing space in some of pdf names noticed during fp print
    file1 = file.split(' ')[0]# removing space continued
else:
    file1 = file[:-4]# removing .pdf
    final = dstDir+"\\"+file1+"\\2018"
    print (srcDir+"\\"+file1+" "+final)
shutil.move(srcDir+"\\"+file,final)
SJCC
  • 1
  • 1