kuldeep sharma
kuldeep sharma

Reputation: 41

Windows directory name is invalid

Here is my code I'm trying this but it keeps repeating the same error. If I am doing something Wrong please tell me.

import os
import sys

for x in sys.argv:
    for ff in os.listdir(x):
        path = os.path.join(x, ff)
        if os.path.isdir(path):
            print('\n--' + path)
        else:
            print('\t------' + path)

This is the error keep repeating, again and again, please someone help me out.

Traceback (most recent call last):
  File "E:/projects/Intern/file&folders/cc.py", line 5, in <module>
    for ff in os.listdir(x):
NotADirectoryError: [WinError 267] The directory name is invalid: 'E:/projects/Intern/file&folders/cc.py'

Upvotes: 1

Views: 3014

Answers (3)

nandal
nandal

Reputation: 2634

Use the following code:-

import os
import sys

for x in sys.argv[1:]:
    for ff in os.listdir(x):
        path = os.path.join(x, ff)
        if os.path.isdir(path):
            print('\n--' + path)
        else:
            print('\t------' + path)

You were passing the whole sys.argv list to os.listdir() function. The first element is always the script itself which is not a directory. Therefore, we sliced the argument list by first element.

Upvotes: 5

d6bels
d6bels

Reputation: 1482

I think the issue may be that some paths contain the character & that isn't well supported.
Try to change that folder name and avoid such characters, or try to skip paths that may contain it.

Upvotes: 1

Bierbarbar
Bierbarbar

Reputation: 1479

I had an an similar error some years ago on a Windowsmachine. I think the Problem is that you path contains an special character (&)... Just change the name of the folder or escape it with backspace

Upvotes: 1

Related Questions