2021-03-30

Python 3: Code is looking for directories that don't exist when it isn't supposed to

I have been working on the following code:

#ZIPPYORACLE USB worm module.
import os, random, stat
#User chosen at random
picked = 0
userchoice = random.choice (os.listdir ("/media/"))
randomdir = random.choice (os.listdir ("/media/" + userchoice))
picked = "/media/" + userchoice + "/" + randomdir
os.chdir (picked)
os.chmod ("usb.txt", stat.S_IWRITE)
with open ("usb.txt", "w") as file:
    print (picked)
    file.write ("Create a new text file!")

It is supposed to upload a file to a random USB drive. The problem is it works only 10% of the time, but 90% of the time gives errors like these:

NotADirectoryError: [Errno 20] Not a directory: '/media/asher/Ubuntu-MATE 20.10 amd64usb.txtusb.txtusb.txtusb.txt'

I have no idea why this is happening, and have been debugging it for an hour now. Do any of you more experienced programmers know what's causing this issue?



from Recent Questions - Stack Overflow https://ift.tt/3cwelka
https://ift.tt/eA8V8J

No comments:

Post a Comment