How Can I Avoid Losing Files When Copying Them with Python?

0
5
Asked By CuriousCoder42 On

I recently wrote a Python script to back up web pages I downloaded from Chrome. I wanted to make sure I had copies in case the originals were deleted. The problem is, I made a mistake with the file paths when copying the files. My program was set up like this:
```
shutil.copy(absolute_filename, absolute_dir)
```
Unfortunately, I entered the wrong source filename, which caused the files to be moved from the Chrome download location to a different directory. Now I've lost about 70 of my downloaded web pages. Does anyone have suggestions for best practices to prevent this from happening again?

3 Answers

Answered By TechieTommy101 On

Definitely consider implementing a dry run option in your scripts. This way, the script will show you what it intends to do without actually moving or deleting any files. That can save you a lot of headaches!

Answered By BackupBuddy99 On

You should also run tests that just report what the script will do instead of executing it right away. It can help catch any major mistakes before they impact your files.

Answered By ScriptSavant88 On

Oof, that sounds frustrating! One tip is to add a confirmation step to your scripts. For example, include a print statement that shows the directories you're working with and then have a prompt like 'press enter to continue' before executing the move. It helps catch mistakes before it's too late.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.