1

I'm trying to write files to a directory in python. The filename is stored in a JSON object. After finding this filename, I match it to a bunch of images in another directory.

If there is a match, I want to write those images to a different directory.

This is what I have so far:

# images is the JSON key which contains the [file_name]
for im in images:
    img_data = im["file_name"]
    # The images are stored in this test directory.
    for root, dirs, files in os.walk("./test/"):
        for filename in files:
            if img_data == filename:
                file_path = os.listdir(directory)
                if not os.path.isdir(directory):
                    os.mkdir(directory)

                file = open(file_path, 'w')
                file.write(filename)
                file.close

With this approach, I'm getting the error for writing to a directory:

File "test-coco.py", line 28, in <module>
    file = open(file_path, 'w')
TypeError: expected str, bytes or os.PathLike object, not list

I'm not sure what I'm doing wrong. Can anyone correct me? Seems like a simple enough problem. (p.s apologies for the horrendous 3 nested for loops)

TLDR trying to write found filename to new-test directory.

Thank you

3
  • please print(file_path) and post what was shown there Commented Aug 20, 2020 at 1:38
  • Sorry misunderstood the question. File path is an empty directory. This is the directory I want to write to: Commented Aug 20, 2020 at 1:40
  • ah, updated my answer Commented Aug 20, 2020 at 1:48

1 Answer 1

1
file = open(file_path, 'w')

crashes because you give it a list which comes from here:

file_path = os.listdir(directory)

a quick fix would be:

file_path = os.listdir(directory)[0]

to get only the first one, but I am not sure that is what you actually want ...


If directory is already a path like this: r"D:\test" you can do this:

import os
directory = r"D:\test"

# images is the JSON key which contains the [file_name]
for im in images:
    img_data = im["file_name"]
    # The images are stored in this test directory.
    for root, dirs, files in os.walk("./test/"):
        for filename in files:
            if img_data == filename:
                if not os.path.isdir(directory):
                    os.mkdir(directory)

                file = open(os.path.join(directory, filename), 'w')
                file.write(filename)
                file.close
Sign up to request clarification or add additional context in comments.

2 Comments

+1 thanks that works. I guess I have another problem where the written images to new directory are empty for some reason but I appreciate your help!
glad i could help! currently you do not write any data to the file besides its filename: file.write(filename) if you want to write a picture you need to have that data somewhere

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.