6

I would like to use the shutil.move() function to move some files which match a certain pattern to a newly created(inside python script)folder, but it seems that this function only works with existing folders.

For example, I have 'a.txt', 'b.txt', 'c.txt' in folder '/test', and I would like to create a folder '/test/b' in my python script using os.join() and move all .txt files to folder '/test/b'

import os 
import shutil
import glob

files = glob.glob('./*.txt') #assume that we in '/test' 

for f in files:
    shutil.move(f, './b') #assume that './b' already exists

#the above code works as expected, but the following not:

import os
import shutil
import glob

new_dir = 'b'
parent_dir = './'
path = os.path.join(parent_dir, new_dir)

files = glob.glob('./*.txt')

for f in files:
    shutil.move(f, path)

#After that, I got only 'b' in '/test', and 'cd b' gives:
#[Errno 20] Not a directory: 'b'

Any suggestion is appreciated!

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
user9875189
  • 179
  • 1
  • 2
  • 10

1 Answers1

7

the problem is that when you create the destination path variable name:

path = os.path.join(parent_dir, new_dir)

the path doesn't exist. So shutil.move works, but not like you're expecting, rather like a standard mv command: it moves each file to the parent directory with the name "b", overwriting each older file, leaving only the last one (very dangerous, because risk of data loss)

Create the directory first if it doesn't exist:

path = os.path.join(parent_dir, new_dir)
if not os.path.exists(path):
   os.mkdir(path)

now shutil.move will create files when moving to b because b is a directory.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Thanks, it works now. I actually made a new directory with os.mkdir(path) in my own project and forgot to write it in this test example. The real problem in my own project is that instead of writing "if not os.path.exists(path), I write just "if not path", which is always false. – user9875189 Aug 14 '19 at 21:48
  • ah that's a classic mistake that happened to everyone. – Jean-François Fabre Jun 16 '22 at 07:51
  • 1
    You can also use os.makedirs(path) to create nested structures – Ron Aug 25 '22 at 16:59