Skip to content

Commit 6d3615b

Browse files
committed
add os.getcwd()
1 parent 1d174c6 commit 6d3615b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

main

64 Bytes
Binary file not shown.

os/os.module.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313

1414
func init() {
1515

16-
methods := []*py.Method{}
16+
methods := []*py.Method{
17+
py.MustNewMethod("getcwd", getCwd, 0, "Get the current working directory"),
18+
}
1719

1820
globals := py.StringDict{
1921
"error": py.OSError,
@@ -41,3 +43,12 @@ func getEnvVariables() py.StringDict {
4143

4244
return dict
4345
}
46+
47+
// gets the current working directory
48+
func getCwd(self py.Object, args py.Tuple) (py.Object, error) {
49+
dir, err := os.Getwd()
50+
if err != nil {
51+
return nil, py.ExceptionNewf(py.OSError, "Unable to get current working directory.")
52+
}
53+
return py.String(dir), nil
54+
}

os/os.test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
22

3-
print(os.error)
4-
print(os.environ.get("HOME")) # prints $HOME variable
3+
print("OS Error ", os.error)
4+
print("$HOME", os.environ.get("HOME")) # prints $HOME variable
5+
print("cwd():", os.getcwd())

0 commit comments

Comments
 (0)