Mode | Description |
---|---|
"r" | Open the file in read-only mode (default) |
"w" | Open the file in write-only mode. Overwrites the file if it exists, or creates a new file if it doesn’t. |
"x" | Open the file in write-only mode. Fails if the file already exists. |
"a" | Open the file in write-only mode. Appends to the end of the file if it exists, or creates a new file if it doesn’t. |
"b" | Open the file in binary mode. |
"t" | Open the file in text mode (default). |
"+" | Open the file for updating (reading and writing). |
Command | Description | Example |
---|---|---|
open(path) | Open a file with the specified path. | f = open("file.txt") |
open(path, mode) | Open a file with the specified path and mode. | f = open("file.txt", "w") |
open(path, mode, encoding) | Open a file with the specified path, mode, and encoding. | f = open("file.txt", "r", encoding="ascii") |
f.close() | Close the file. | f.close() |
with open(path) as f: | Open a file with the specified path and automatically close it when the block ends. | with open("file.txt") as f: |
f.read() | Read the contents of the file. | contents = f.read() |
f.read(size) | Read the first size characters of the file. | contents = f.read(10) |
f.write(string) | Write a string to the file. | f.write("Hello, World!") |
f.writelines(lines) | Write a list of strings to the file. | lines = ["line 1", "line 2", "line 3"]; f.writelines(lines) |
f.seek(offset) | Move the current position to the specified offset. | f.seek(0) |
f.seek(offset, whence) | Move |
Command | Description | Example |
---|---|---|
f.tell() | Get the current position in the file. | position = f.tell() |
f.readline() | Read a single line from the file and return it as a string. | line = f.readline() |
f.readlines() | Read all lines of the file and return them as a list of strings. | lines = f.readlines() |
f.truncate(size) | Truncate the file to the specified size (in bytes). | f.truncate(100) |
f.flush() | Flush any unwritten data to the file. | f.flush() |
f.isatty() | Return True if the file is connected to a terminal, False otherwise. | result = f.isatty() |
f.closed | Return True if the file is closed, False otherwise. | result = f.closed |
os.mkdir(path) | Create a new directory at the specified path. | os.mkdir("/new/directory") |
os.rmdir(path) | Remove the specified directory. | os.rmdir("/directory") |
os.chdir(path) | Change the current working directory to the specified path. | os.chdir("/new/working/directory") |
os.getcwd() | Return the current working directory. | cwd = os.getcwd() |
os.listdir(path) | Return a list of the files and directories in the specified directory. | items = os.listdir("/directory") |
os.removedirs(path) | Remove a directory and all its parent directories if they are empty. | os.removedirs("/directory/to/remove") |
Command | Description | Example |
---|---|---|
os.path.exists(path) | Return True if the specified path exists, False otherwise. | result = os.path.exists("/path/to/check") |
os.path.isfile(path) | Return True if the specified path is a file, False otherwise. | result = os.path.isfile("/path/to/check") |
os.path.isdir(path) | Return True if the specified path is a directory, False otherwise. | result = os.path.isdir("/path/to/check") |
os.path.join(path1, path2, ...) | Join multiple paths into a single path. | full_path = os.path.join("/path/to", "join", "to/this") |
os.path.split(path) | Split a path into a tuple of the head and tail. | result = os.path.split("/path/to/split") |
os.path.splitext(path) | Split a path into a tuple of the root and extension. | result = os.path.splitext("/path/to/split.txt") |
os.path.dirname(path) | Return the directory name of the |