Python Forum
Shutil FileNotFoundError: Errno 2 Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shutil FileNotFoundError: Errno 2 Help
#1
Hi,

I've recently started learning how to code in Python.

I currently have a list of filenames in a text file (Source Text File). I also have a list of folder locations in another text file (Destination). I am trying to have Python read the source text file and move the corresponding file with that name to the destination folder by reading the corresponding folder path line in the destination text file.

I am not quite sure how to go about reading the corresponding destination line to move it to the relevant folder and subfolder. Whilst running the code below I also seem to be getting an error:

FileNotFound error: [Errno 2] No such file or directory.

Any help on how to resolve this or if there are any tutorials on something similar would be appreciated.

import shutil
import os

cwd = os.getcwd()
loc = "/Code/List.txt"
dest = "/Code/Test Copy"
srce = '/Code/Docs'

#my_file = open(loc, "r")
#content = my_file.readlines()
#content_list = content.split(",")
# my_file.close()
# print(content)

with open(loc) as my_file:
    for filename in my_file:
        file_name = filename.strip()
        src = r'/Code/Docs' + file_name
        shutil.move(src, dest + '\'' + file_name)
Reply
#2
Instead of calling shutil.move I would call print to take a look at your source and destination path/file names I'm pretty sure neither are correct.
Reply
#3
Ok,

So after some trial error, I managed to fix that error. Following on from your tip deanhystad the folders had special characters which is what the issue was about.

I've run into another issue. When trying to copy the first file to correct destination in the destination text file it copies the first file across all folders.

Any tips on howe to resolve this?

import shutil
import os

cwd = os.getcwd()
loc = "/Code/List.txt"
dest = "/Code/Test Copy/"
srce = '/Code/Docs'
othdest = "/Code/Destr.txt"

#my_file = open(loc, "r")
#content = my_file.readlines()
#content_list = content.split(",")
# my_file.close()
# print(content)

with open(loc) as my_file:
    for filename in my_file:
       # print(filename)
        file_name = filename.strip()
        src = r'/Code/Docs/' + file_name
        with open(othdest) as my_destination:
            for filedestname in my_destination:
               # print(filedestname)
                dorc = filedestname.strip()
               # dorc = r'/Code/Test Copy'+filedestname
              # print(dorc)
                shutil.copy(src, dorc + file_name)
Reply
#4
Are you surprised? What else do you think your logic would do:
for each filename in filenames_file
    for each directory in directories_file
        copy source_dir/filename to directory/filename
The inner loop for each directory in directories_file copies the source file to each of the destination directories before moving on to the next filename.

If you want to do a kind of shuffle logic where the first filename is matched to the first directory, the second filename to the second directory and so on you need to remove the inner loop.
for each filename in filenames_file
    get directory from directories_file
    copy source_dir/filename to directory/filename
Reply
#5
Hi deanhystad,

Thanks for your help thus far. Please bear with me as I'm still trying to understand the logic.

I have source text file with a list of all file names in it. I also have a destination text file with each path for each file read from the source text file and where it needs to go.

Wouldn't I need to have 2 for loops? The first for loop is simply reading the contents of the source text file whereas the 2nd for loop is reading the corresponding path from the destination text file.

I can understand that the issue is where the placement of the 2nd for loop is but then how would I read and pass that destination path from the destination text file to 1st for loop?

Thanks & Appreciate the help so far!
Reply
#6
Maybe I don't understand the problem, but I thought you have a file containing filenames.
Quote:name1
name2
name3
And you also have file containing file paths.
Quote:path1
path2
path3
And that you want to use the names and paths to copy files from a source folder to different destination folders.
Quote:copy source_dir/name1 to path1/name1
copy source_dir/name2 to path2/name2
copy source_dir/name3 to path3/name3
If this is correct I see no reason for two loops. In one loop you could read the filename, read the desination directory and dp tje file copy (or rename).

I suppose you could use two loops. A loop to read all the file names and then a second loop to read the destination directory and do the copy. These loops would be one after the other, not one nested inside another. Heck, you could use three loops if you wanted; read names, read paths, do copy/rename. But why use 2 or 3 loops when one loop is faster and makes the most sense. One loop with a series of tasks that are performed for each loop iteration.
Reply
#7
Hi deanhystad,

Yes, that is exactly what I am after but I'm having issues with reading the destination path in the same loop.

Your suggestion says:

get directory from directories_file
Isn't it supposed to read each path line in the destination text file? This is where my issue is not knowing how to pass that through. How would I write this syntax?

Appreciate the help
Reply
#8
If you don't know how files work read the documentation.

https://docs.python.org/3/tutorial/input...ting-files
Reply
#9
After reading through that document, I finally got it to work.

Thanks for the help!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of shutil.copytree with ENOTDIR exception yan 2 897 Nov-29-2023, 03:02 PM
Last Post: yan
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,561 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  shutil.move make data corrupt kucingkembar 0 788 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,631 Sep-12-2022, 04:50 AM
Last Post: python202209
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,584 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  FileNotFoundError: [Errno 2] No such file or directory: 'word2vec.model' Anldra12 6 6,024 Jul-07-2021, 07:29 AM
Last Post: Anldra12
  FileNotFoundError: [Errno 2] No such file or directory: 'model/doc2vec.model/Articles Anldra12 10 5,773 Jun-11-2021, 04:48 PM
Last Post: snippsat
  extra slashes in the network path for shutil.copy tester_V 3 3,760 Jun-02-2021, 07:57 AM
Last Post: supuflounder
  concatenat - shutil jmabrito 3 2,179 Feb-11-2021, 12:17 PM
Last Post: jmabrito
  FileNotFoundError: [Errno 2] No such file or directory: 5l3y3r 6 8,106 Nov-02-2020, 12:48 PM
Last Post: 5l3y3r

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020