0

I'm trying to simply move files from folder path1 to folder path.

import os
import shutil
path1 = '/home/user/Downloads'
file_dir = os.listdir(path1)
fpath = '/home/user/music'
for file in file_dir:
    if file.endswith('.mp3'):
        shutil.move(os.path.join(file_dir,file), os.path.join(fpath, file))

... but I get this error

TypeError: expected str, bytes or os.PathLike object, not list
3
  • The method listdir() returns a list containing the names of the entries in the directory given by path. source. Do file_dir = os.listdir(path1)[0] perhaps? Commented Oct 4, 2018 at 16:36
  • 2
    (os.path.join(file_dir,file)here you are trying to join a list with a string, check this line again. Commented Oct 4, 2018 at 16:36
  • (os.path.join(file_dir,file) should be (os.path.join(path1,file) I believe. Commented Oct 4, 2018 at 16:38

3 Answers 3

2

First of all, you shouldn't use file as a variable name, it's a builtin in python, consider using f instead.

Also notice that in the shutil.move line, I've changed your (os.path.join(file_dir,f) to (os.path.join(path1,f). file_dir is a list, not the name of the directory that you're looking for, that value is stored in your path1 variable.

Altogether, it looks like this:

import os
import shutil
path1 = '/home/user/Downloads'
file_dir = os.listdir(path1)
fpath = '/home/user/music'
for f in file_dir:
    if f.endswith('.mp3'):
        shutil.move(os.path.join(path1,f), os.path.join(fpath, f))
Sign up to request clarification or add additional context in comments.

1 Comment

I guess it depends on 2.x vs 3.x with the use of file, but for backwards compatibility (who does that?) it should probably be avoided.
1

You have confused your variable purposes from one line to the next. You've also over-built your file path construction.

You set up file_dir as a list of all the files in path1. That works fine through your for command, where you iterate through that list. The move method requires two file names, simple strings. Look at how you construct your file name:

os.path.join(file_dir,file)

Remember, file_dir is a list of files in path1. file is one of the files in that list. What are you trying to do here? Do you perhaps mean to concatenate path1 with file?

NOTE: Using pre-defined names as variables is really bad practice. file is a pre-defined type. Instead, use f or local_file, perhaps.

Comments

0

Read carefully the error message. file_dir is list. You can not join it with os.path.join. You probably want to write:

shutil.move(os.path.join(path1, f), os.path.join(fpath, f))

I suggest to name variables with meaningful names like:

file_list = os.listdir(path1)

This way you will not join a file list with a path :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.