2019-07-11 18:37:38 +02:00
|
|
|
---
|
|
|
|
title: 'os'
|
|
|
|
---
|
2019-07-10 01:54:15 +02:00
|
|
|
|
2019-07-11 18:37:38 +02:00
|
|
|
os provides a platform-independent interface to operating system functionality.
|
2020-03-23 21:18:06 +01:00
|
|
|
|
|
|
|
## Index
|
|
|
|
|
|
|
|
|
|
|
|
* [def <b>chdir</b>(dir)](#def-iosibchdirb)
|
|
|
|
* [def <b>getenv</b>(key) dir](#def-iosibgetenvb)
|
|
|
|
* [def <b>getwd</b>() dir](#def-iosibgetwdb)
|
|
|
|
* [def <b>mkdir</b>(name, perms=0o777)](#def-iosibmkdirb)
|
|
|
|
* [def <b>mkdir_all</b>(name, perms=0o777)](#def-iosibmkdir_allb)
|
|
|
|
* [def <b>read_file</b>(filename) string](#def-iosibread_fileb)
|
|
|
|
* [def <b>remove</b>(name)](#def-iosibremoveb)
|
|
|
|
* [def <b>remove_all</b>(path)](#def-iosibremove_allb)
|
|
|
|
* [def <b>rename</b>(oldpath, newpath)](#def-iosibrenameb)
|
|
|
|
* [def <b>setenv</b>(key, value) dir](#def-iosibsetenvb)
|
|
|
|
* [def <b>write_file</b>(filename, data, perms=0o644)](#def-iosibwrite_fileb)
|
|
|
|
|
|
|
|
|
2019-07-10 01:54:15 +02:00
|
|
|
## Functions
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>chdir</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.chdir(dir)
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
changes the current working directory to the named directory.
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `dir` | `string` | target dir |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>getenv</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.getenv(key) dir
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
retrieves the value of the environment variable named by the key.
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `key` | `string` | name of the environment variable |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>getwd</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.getwd() dir
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
returns a rooted path name corresponding to the current directory.
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>mkdir</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.mkdir(name, perms=0o777)
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
creates a new directory with the specified name and permission bits (before umask).
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `name` | `string` | name of the folder to be created |
|
|
|
|
| `perms` | `int` | optional, permission of the folder |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>mkdir_all</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.mkdir_all(name, perms=0o777)
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
creates a new directory with the specified name and permission bits (before umask).
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `name` | `string` | name of the folder to be created |
|
|
|
|
| `perms` | `int` | optional, permission of the folder |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>read_file</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.read_file(filename) string
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
reads the file named by filename and returns the contents.
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `filename` | `string` | name of the file to be written |
|
|
|
|
| `data` | `string` | content to be witten to the file |
|
|
|
|
| `perms` | `int` | optional, permission of the file |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>remove</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.remove(name)
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
removes the named file or (empty) directory.
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `name` | `string` | name of the file or directory to be deleted |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>remove_all</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.remove_all(path)
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2020-03-26 21:26:03 +01:00
|
|
|
removes path and any children it contains. It removes everything it
|
|
|
|
can but returns the first error it encounters.
|
2019-07-10 01:54:15 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `name` | `string` | path to be deleted |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>rename</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.rename(oldpath, newpath)
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2020-03-26 21:26:03 +01:00
|
|
|
renames (moves) oldpath to newpath. If newpath already exists and is
|
|
|
|
not a directory, Rename replaces it. OS-specific restrictions may
|
|
|
|
apply when oldpath and newpath are in different directories.
|
2019-07-10 01:54:15 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `oldpath` | `string` | old path |
|
|
|
|
| `newpath` | `string` | new path |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>setenv</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.setenv(key, value) dir
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
sets the value of the environment variable named by the key.
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `key` | `string` | name of the environment variable |
|
|
|
|
| `value` | `string` | value of the environment variable |
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|
2019-07-11 18:45:42 +02:00
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
#### def <i>os</i>.<b>write_file</b>
|
2019-07-11 18:37:38 +02:00
|
|
|
```go
|
2020-03-23 21:18:06 +01:00
|
|
|
os.write_file(filename, data, perms=0o644)
|
2019-07-11 18:37:38 +02:00
|
|
|
```
|
2019-07-10 01:54:15 +02:00
|
|
|
retrieves the value of the environment variable named by the key.
|
|
|
|
|
2020-03-23 21:18:06 +01:00
|
|
|
###### Arguments
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------|------|-------------|
|
2020-03-23 21:18:06 +01:00
|
|
|
| `filename` | `string` | name of the file to be written |
|
|
|
|
| `data` | `string` | content to be witten to the file |
|
|
|
|
| `perms` | `int` | optional, permission of the file |
|
2019-07-11 18:37:38 +02:00
|
|
|
|
2019-07-10 01:54:15 +02:00
|
|
|
|
|
|
|
|