Kristopher
Kristopher

Reputation: 3

Attempting to move files with certain names to a new directory

I am attempting to move files that start with PS-110 to a new folder I created named PS-110.

import shutil, glob, os

files_move = []
files_move = [files_move.append(f) for f in glob.glob('PS-110*.pdf')]

destination = r"C:\Users\kjurgens\Downloads\PS-110"

for f in files_move:
        shutil.move(f, destination)

When it runs I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 771, in move
    if _samefile(src, dst):
  File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 217, in _samefile
    return os.path.samefile(src, dst)
  File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\genericpath.py", line 100, in samefile
    s1 = os.stat(f1)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

Any input would be greatly appreciated.

Upvotes: 0

Views: 43

Answers (1)

blueteeth
blueteeth

Reputation: 3555

files_move = [files_move.append(f) for f in glob.glob('PS-110*.pdf')]

This doesn't create a list of files, it creates a list full of None. This is because files_move.append(f) returns None. And you're overwriting files_move with the new list by the time this whole execution finished.

Given glob.glob() already returns a list, you don't need files_move at all.

Just do:

import shutil, glob, os

destination = r"C:\Users\kjurgens\Downloads\PS-110"

for f in glob.glob('PS-110*.pdf'):
    shutil.move(f, destination)

Upvotes: 1

Related Questions