0

Python beginner here.

I am developing a little python script which is copying files from the PC to a USB.

Now. The .py file is in the "MyPythonScript" folder and it should copy files in that directory to the usb. and my Script looks something like this.

username = getpass.getuser()
src = 'C:/Users/' + username + '/Desktop/MyPythonScript/testfolder/sometext.txt
dst = 'F:'
shutil.copy(src, dst)

It works perfectly fine, but it assumes that the Folder name is MyPythonScript and it's located on the Desktop. What do I need to do to get the data right out of the folder where the python script is located? So it doesn't matter if it is in Desktop, Downloads, or anywhere on another hard drive.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Lukas
  • 1
  • Does this answer your question? [How do I copy a file in Python?](https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) – random_hooman Aug 26 '21 at 08:38

1 Answers1

0

You can loop through every folder with os.listdir() and check the name of the file with if-else statements than copy the file. Im not sure its the best practice because it will be too slow and you can have 2 files in different folders with the same name i dont know how to prevent this problem.

reisgoldmanX
  • 60
  • 1
  • 7