shutil.move() only works with existing folder?
Asked Answered
L

1

7

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!

Lyautey answered 14/8, 2019 at 21:22 Comment(0)
R
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.

Removed answered 14/8, 2019 at 21:27 Comment(3)
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.Lyautey
ah that's a classic mistake that happened to everyone.Litchfield
You can also use os.makedirs(path) to create nested structuresBruell

© 2022 - 2024 — McMap. All rights reserved.