Using Python 3.8 on Windows 10 OS. The first time I run copy_tree it works perfectly, i.e. all files and sub-directories are copied over as expected.
When I manually delete the newly copied directory from the destination and run copy_tree again (within 1 minute from deleting the initial destination directory created by copy_tree), it will copy everything but the sub-directories. Note that there is no content within these sub-folders.
Is there a timeframe where a deleted folder is not technically deleted?
EDIT: Here are my functions:
def isFolder(path):
return os.path.isdir(path) if os.path.exists(path) else False
def copyFolderContent(srcFolder, destFolder):
srcFolderOK = isFolder(srcFolder)
destFolderOK = isFolder(destFolder)
if srcFolderOK and destFolderOK:
return copy_tree(srcFolder, destFolder)
else:
return None
The source directory looks like this:
SourceParentFolder
-> ChildFolder
The destination directory looks like this:
DestinationParentFolder
(no contents)
After running copy_tree the destination directory is:
DestinationParentFolder
-> ChildFolder
Then, after manually deleting "ChildFolder" and running copy_tree within approximately one minute:
DestinationParentFolder
(no contents)
The expectation is that "ChildFolder" is copied into DestinationParentFolder, but this does not happen unless a significant amount of time has passed.
I am having a very similar, perhaps identical, problem. After some testing, it seems the issue might partly be with my IDE, Spyder.
The Issue
I have two directories
/test/aand/test/x. I want to copy the contents ofaintox. I want preserve as much metadata as possible, and I don't want my script to fail for existing files (as was the case in this question), so I'm trying to usedistutils.dir_util.copy_tree().acontains the following files and subdirectories:Yesterday, I created a new anaconda environment with Python 3.9.2 and Spyder 4.2.3.
I ran the following script in Spyder:
The first time I run the script, all of
a's contents are copied intox.If I delete
xand re-run the script, a newxis created, the contents ofaare not copied, and the following errors are thrown:The error seems to be caused by _copy_file_contents, which is trying to open and write a new file where none exists. This doesn't make sense to me, because there shouldn't be a file there in the first place. See this unanswered question for a very similar problem, but with
shutil.copytree()instead.What I tried
I tried deleting
xagain, then exiting out of Spyder, starting a new anaconda terminal session, opening Spyder again, and re-running the script. Everything works as expected (xis created, anda's contents are copied into it). If I subsequently deletexand re-run the script,xis created and then the same errors are thrown. I don't know why restarting the Anaconda prompt and Spyder solves the problem for the first script run, but I was able to reproduce this multiple times.I tried moving
aand my script file into a new parent directory, and the script worked again (createdx, copied all ofa, etc). In the new location, I then deletedxand re-ran the script.xwas created and then the same errors were thrown.I then deleted
x, exited out of Spyder, and started IDLE from the Anaconda prompt. I ran the script in IDLE and it worked the first time. I deletedxand re-ran the script, and it worked again. I did this several more times and it worked each time.Why is my script working in IDLE but not in Spyder?
My apologies for my ridiculously long answer/question. I didn't want to post a separate question, as this sounded like a duplicate. Hopefully, this issue will get some more attention.
System Info: Windows 10, Python 3.9.2, Spyder 4.2.3