#๐Ÿ”’ New to python, wrote a simple file-copying script and it seemed to work fine, until!

43 messages ยท Page 1 of 1 (latest)

winged irisBOT
#

@quasi meteor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quasi meteor
#

well darn, that was a long message to have deleted. Any way to recover just the text of the message and I can send the script correctly?

mellow pivot
#

don't think so, but to paste large amounts of code:

#

!code

winged irisBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

mellow pivot
#

!paste

winged irisBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

quasi meteor
#

So, I am trying to copy files from old DVDs from a few decades ago, onto my laptop.
File Explorer was being a pain, and kept hanging randomly when it tried copying certain files.
and even worse, it only copied some of the files, leaving random gaps in my folder!

I didn't want to manually check all the filenames to see what to copy,
so I wrote a quick python script so that I would be able to copy the files faster without messing with File Explorer.
[Note: This script does not validate whether or not the files are equivalent, all it does is check the filenames.]

Something odd is happening. When my script copies .jpg or .mov files, occasionally, it will copy a file that is 2.45 GB!
What a specific number! I cannot open these JPGs, but now, I have dozens of 2.45 GB corrupted JPGs on my computer ๐Ÿ˜ฑ whoops
I am guessing somehow, there's just no end of file to the jpgs or something, and it's somehow copying the entire disk in as if it were a jpg?
I am not sure! It's rather funny though ๐Ÿ˜‚

Here is my script: https://paste.pythondiscord.com/BUCQ
(btw, I am new to the writing standards of python so if you have any feedback feel free to leave it too! happy to learn)
DVD Drive: Sandisk Ultra Slim Portable DVD Writer
Destination folder: D:/, m.2 2280 ssd

Is there something I can do to make my code safer and prevent it from attempting to copy files past a certain filesize limit?
Occasionally, the copying process will hang even in my script, and after 10 minutes, it will return
Problem copying IMG_7568.JPG: [Errno 13] Permission denied.
and the file will be copied partially like the attached image.

I would very much love to recover the photos that consistently corrupt like this, since they're baby photos!
Any help appreciated, thank you so much!

sharp folio
quasi meteor
#

Yep, just had a mod help me with that, ty!

sharp folio
quasi meteor
#

Also, for more information:
My code assumes that the entire directory is nothing but files, it's not built to handle sub directories or anything like that,
since the disc I am copying from has no subdirectories in it (I might add code for handling that later)

In summary there are two unexpected problems that happen with my code:

  • Occasionally the program will copy files that are multiple gigabytes in size larger than they should be.
    The size of every file copied is oddly consistently 2.45 GB. I just checked, and for a previous disk, there was the same problem,
    except consistently 1.78 GB.

  • Occasionally, the program will hang for 10-30 minutes, then print the error:
    Problem copying {filename}. [Errno 13] Permission denied. (see line 102 in script)
    I do not understand the "Permission denied" error code exactly, since it seems to copy the file partially all good,
    until it hangs. Really odd problems!

sharp folio
#

(btw, I think you reinvented rsync)

quasi meteor
sharp folio
#

2.45GB sounds like it might be the size of the DVD disk (or one track of it, something like that)

quasi meteor
#

oh nice, haha
I might check that out! I figure someone out there has already made something like this before I just wanted to make it myself ;joy

quasi meteor
sharp folio
#

One thing you could try is copying manually - opening source and destination files, reading out, and writing into the other. That's inefficient compared to how copying files works normally, but it seems to me like it's also harder for it to go wrong

quasi meteor
#

Huh, just realized the files show up that size in the E: drive itself

#

so yeah this is definitely an issue of the disk reader and not the python script, darn

sharp folio
#

ah, then my idea won't help

#

to save the files from this nightmare drive you'll have to know their real sizes, somehow.

#

which... might actually be possible, since they're images

quasi meteor
#

I'll keep that idea in mind for the other problem, though, see if it helps with the files that are half chopped off

sharp folio
#

what format are these images in?

quasi meteor
#

.JPG and .MOV

#

in all caps like that if it matters

sharp folio
#

hmm, lemme experiment a little. I suspect if you tell ffmpeg to copy a jpg/mov and the file has garbage data at the end, it'll just ignore it

quasi meteor
#

||tangent that doesn't matter: one thing I just found out while browsing the docs, apparently .copy() doesn't preserve metadata but .copy2() does,
but somehow, my metadata is being preserved anyway even though I'm using .copy()? That's good since I didn't intend to wipe metadata, but interesting!||

sharp folio
#

so you can in theory copy all the image/video files that way, possibly losing some quality from the reencoding

#
def ffmpeg_transfer(src_path, dest_path):
    "The destination path must have an extension (that's how ffmpeg knows what format to output.)"
    subprocess.run(
        [
            "ffmpeg",
            "-i",
            src_path,
            dest_path,
            "-y",
        ]
    )

something like that. You'll also need ffmpeg of course (it's a CLI tool; on windows the simplest way to install it would be via Chocolatey, though you can also just download the binaries, unpack them somewhere, and use the absolute path to ffmpeg.exe instead of "ffmpeg" in this snippet).

quasi meteor
#

ah ye I use ffmpeg occasionally to convert weird files to mp3!
I downloaded chocolatey awhile ago because someone told me to in a tutorial for glsl,
but I don't actually know what it is cause I haven't gotten past the first few minutes lmfao.
I just know it freaked me out when it started running this big intimidating powershell out of nowhere ๐Ÿ˜‚

sharp folio
#

it's a package manager for windows (one of several existing ones). Lets windows users have the linux experience of installing things by just writing something like choco install ffmpeg.

quasi meteor
#

ah, ok! so kinda like nuget

#

good to know

#

Just realized the sizes of the files are actually mildly different. interesting!

#

makes me wonder if I downloaded the largest one, there was some potential of like. parsing it somehow, and that magically recovering the entire disc haha.

sharp folio
#

though normally files get fragmented... but maybe on CD drives they don't, since they are typically written in one shot.

sharp folio
#

like, directly copying the disk to a file, and then converting it to iso, or something like that

winged irisBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.