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.
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) ```
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.
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.
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
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.
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)
```
Will not work correctly if the filename has 2 or more dots
You're better off using pathlib to assemble the destination
My teacher ❤
Great tutorial as always! That you 🙏🏼
Can you please talk about file and directory paths im having a some trouble using them
Thank you 😊
Yo can you also make a tutorial on os.walk() ? 😅
Thanks
Wow, what a great function name, `copy2`!!! 😆
I agree xD
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??
The video is only to copy png file. It would have been nice if it applied to the files of other extensions
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.
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.
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
@@James-ln6lithat is true for mac and linux… cant day the same about windows
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
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.
@@Indently Don't worry, I will post a comment with the code for that.