0

I'm getting these errors:

Traceback (most recent call last):
  File "file_mover.py", line 41, in <module>
    shutil.copy(f, os.path.join(path_to_export_two, Path("/image/")))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\shutil.py", line 245, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '8ballshake1@8x.png'

When executing this code:

# Making the substrings is more difficult then anticipated prolly just use Java tbh
# LOWERCASE, LOWERCASE THE FOLDERS
import shutil
import os
from pathlib import Path

assets_path = Path("/Users/Jackson Clark/Desktop/uploads")
export_path = Path("/Users/Jackson Clark/Desktop/uploads")

source = os.listdir(assets_path)

"""
NOTE: Filters.js is the important file
The logic:
    - Go through each file in the assets_path directory
    - Rename the files to start with RoCode (this could be a seperate script)
    - Create a new directory with the first four characters of the files name
    - Create two sub directories with the names 'image' and 'thumb'
    - Copy the file to both the 'image' and 'thumb' directories
    That should be all, but who knows tbh
"""
"""
Good links:
    https://www.pythonforbeginners.com/os/python-the-shutil-module
    https://stackabuse.com/creating-and-deleting-directories-with-python/
"""

for f in source:
    f_string = str(f)
    folder_one_name = f_string[0:2]
    folder_two_name = f_string[2:4]

    path_to_export_one = os.path.join(export_path, folder_one_name)
    path_to_export_two = os.path.join(export_path, folder_one_name, folder_two_name)

    os.mkdir(path_to_export_one)
    os.mkdir(path_to_export_two)
    os.mkdir(os.path.join(path_to_export_two, Path("/image/")))
    os.mkdir(os.path.join(path_to_export_two, Path("/thumb/")))

    shutil.copy(f, os.path.join(path_to_export_two, Path("/image/")))
    shutil.copy(f, os.path.join(path_to_export_two, Path("/thumb/")))

I simply need the code to generate two folders, the first being named the first two characters of the file that the script is reading, and the second folder (which is a subfolder of the first) to be named the 3rd and 4th characters of the filename that the script is reading. Alas, I'm getting the errors above.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104

1 Answers1

0

Get rid of the leading slashes in your Paths. These are causing the paths to be truncated:

>>> print(os.path.join("some_folder", Path("/image/")))
\image
>>> print(os.path.join("some_folder", Path("image")))
some_folder\image

The relevant sentence of the os.path.join documentation is as follows:

If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

The leading slash in /image/ causes this path component to become absolute and hence the previous components (in my case, just "some_folder") are discarded.

Also, I don't see why you're creating a Path from a string when you can just use a string directly:

>>> print(os.path.join("some_folder", "image"))
some_folder\image
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • Still having problems with a FileNotFoundError when trying to copy the files. – Jackson Clark May 09 '19 at 21:13
  • @JacksonClark: sorry, but you don't make it clear whether the error you're getting is the same as before or a different one. You've also accepted my answer, so has your problem been solved or not? – Luke Woodward May 10 '19 at 20:44