How To Copy Files (Python Recipes)

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024

ความคิดเห็น • 21

  • @anon_y_mousse
    @anon_y_mousse 2 หลายเดือนก่อน +1

    It's kind of funny that I just watched a video on C++ the other day that showed off using the filesystem library which also overloads operator/ to work as a path separator. Looking at the history, Python actually beat C++ to the punch by 3 years. This is why people need to keep learning because when designing my own library I thought it was unique and now I find out it's been standard for quite a few years in multiple languages.

  • @foxypiratecove37350
    @foxypiratecove37350 2 หลายเดือนก่อน +3

    Here's an improved version that works with every file type:
    ```
    from tkinter import filedialog
    from pathlib import Path
    from shutil import copy2
    src_path: Path = Path(filedialog.askopenfilename(title='Pick a file to copy'))
    dst_dir: str = filedialog.askdirectory(title='Pick the destination directory')
    dst_path: Path = Path(dst_dir) / f'{src_path.stem} - Copy.{src_path.suffix}'
    copy2(src=src_path, dst=dst_path)
    ```

    • @radoeka
      @radoeka 2 หลายเดือนก่อน +2

      Will not work correctly if the filename has 2 or more dots

    • @MatthewMakesAU
      @MatthewMakesAU 2 หลายเดือนก่อน

      You're better off using pathlib to assemble the destination

  • @cocoatea57
    @cocoatea57 2 หลายเดือนก่อน +2

    My teacher ❤

  • @puzobaklan
    @puzobaklan 2 หลายเดือนก่อน +1

    Great tutorial as always! That you 🙏🏼

  • @tarindell187
    @tarindell187 2 หลายเดือนก่อน

    Can you please talk about file and directory paths im having a some trouble using them

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 2 หลายเดือนก่อน +1

    Thank you 😊

  • @lazy_lofi223
    @lazy_lofi223 2 หลายเดือนก่อน +1

    Yo can you also make a tutorial on os.walk() ? 😅

  • @rishiraj2548
    @rishiraj2548 2 หลายเดือนก่อน

    Thanks

  • @farzadmf
    @farzadmf 2 หลายเดือนก่อน +2

    Wow, what a great function name, `copy2`!!! 😆

    • @Indently
      @Indently  2 หลายเดือนก่อน +3

      I agree xD

  • @yashbanait...7277
    @yashbanait...7277 2 หลายเดือนก่อน

    How python can be used in industrial level and as it is scripting language, does it have a value in market after completion of it??

  • @casualchou
    @casualchou 2 หลายเดือนก่อน +6

    The video is only to copy png file. It would have been nice if it applied to the files of other extensions

    • @Indently
      @Indently  2 หลายเดือนก่อน +4

      It was a demo, preferably you will scrape the original file name and extension and add "-copy" to the original name or something, but that goes beyond the scope of the recipe.

    • @James-ln6li
      @James-ln6li 2 หลายเดือนก่อน +2

      You could easily modify the code to get the extension via Pathlib for the file to select. The same copy code via shutil would apply to any file type. He chose png to make the code simple. The file extension is just a hint to the OS as to what the data in the file is.
      If you took the same code and selected a txt file it would create copy.png. if you opened copy.png into a text editor it would display the text from the original file.

    • @Indently
      @Indently  2 หลายเดือนก่อน +2

      I was actually disappointed when I tried renaming a folder to folder.png, and it didn't actually try to convert it to a png

    • @eduferreyraok
      @eduferreyraok 2 หลายเดือนก่อน

      @@James-ln6lithat is true for mac and linux… cant day the same about windows

  • @tenkokan542
    @tenkokan542 2 หลายเดือนก่อน +1

    There a problem that you didn't explain
    If you copy a source.pdf it will make a copy.png, that how shutil work if i remember correctly. Yes, there is a way to do it correctly and it's not that simple(not that hard, but a little complicated) maybe that's why you didn't put it in the vide, but you should put it as an exception there

    • @Indently
      @Indently  2 หลายเดือนก่อน

      That's a fair point, it's true that if you make a copy you should make sure to provide the correct extension for it as well.

    • @foxypiratecove37350
      @foxypiratecove37350 2 หลายเดือนก่อน

      @@Indently Don't worry, I will post a comment with the code for that.