Skip to content

Add os.listdir #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add os.listdir
  • Loading branch information
reyoung committed Jan 16, 2023
commit e88cd47f31f3e226bae8602c6ae48269bf531a31
57 changes: 57 additions & 0 deletions stdlib/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func init() {
py.MustNewMethod("rmdir", rmdir, 0, rmdir_doc),
py.MustNewMethod("system", system, 0, "Run shell commands, prints stdout directly to default"),
py.MustNewMethod("unsetenv", unsetenv, 0, "Unset (delete) the environment variable named key."),
py.MustNewMethod("listdir", listDir, 0, listDir_doc),
}
globals := py.StringDict{
"error": py.OSError,
Expand Down Expand Up @@ -517,3 +518,59 @@ func system(self py.Object, args py.Tuple) (py.Object, error) {

return py.Int(0), nil
}

const listDir_doc = `
Return a list containing the names of the files in the directory.

path can be specified as either str, bytes. If path is bytes, the filenames
returned will also be bytes; in all other circumstances
the filenames returned will be str.
If path is None, uses the path='.'.

The list is in arbitrary order. It does not include the special
entries '.' and '..' even if they are present in the directory.
`

func listDir(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
var (
path py.Object = py.None
)
err := py.ParseTupleAndKeywords(args, kwargs, "s:listdir", []string{"path"}, &path)
if err != nil {
return nil, err
}

if path == py.None {
cwd, err := os.Getwd()
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "cannot get cwd, error %s", err.Error())
}
path = py.String(cwd)
}

dirName := ""
returnsBytes := false
switch v := path.(type) {
case py.String:
dirName = string(v)
case py.Bytes:
dirName = string(v)
returnsBytes = true
default:
return nil, py.ExceptionNewf(py.TypeError, "str or bytes expected, not %T", path)
}

dirEntries, err := os.ReadDir(dirName)
if err != nil {
return nil, py.ExceptionNewf(py.OSError, "cannot read directory %s, error %s", dirName, err.Error())
}
result := py.NewListSized(len(dirEntries))
for i, dirEntry := range dirEntries {
if returnsBytes {
result.Items[i] = py.Bytes(dirEntry.Name())
} else {
result.Items[i] = py.String(dirEntry.Name())
}
}
return result, nil
}
2 changes: 2 additions & 0 deletions stdlib/os/testdata/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
os.mkdir(dir1)
os.mkdir(dir2)
os.mkdir(dir11)
print(os.listdir(top))
os.removedirs(dir1)
try:
os.mkdir(dir11)
Expand Down Expand Up @@ -181,6 +182,7 @@
print("INVALID error caught: %s" % e)
os.remove(fname)
os.rmdir(dir2)
print(os.listdir(top))
except Exception as e:
print("could not create/remove directories: %s" % e)
finally:
Expand Down
2 changes: 2 additions & 0 deletions stdlib/os/testdata/test_golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ os.linesep: [OK]
os.devnull: [OK]
os.altsep: [OK]
caught: OSError: 'Bad file descriptor' [OK]
['dir1', 'dir2']
caught: SystemError - no such file or directory [OK]
caught: FileExistsError [OK]
caught: SystemError - directory not empty [OK]
['dir1']
os.{mkdir,rmdir,remove,removedirs} worked as expected
OK