Python Book
πŸ‡ΊπŸ‡¦ Stand with UkraineπŸŽ“Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page

Was this helpful?

Edit on Git
  1. 10. System Libs

Working with files

File (file descriptor) in Python is object (as everything else)

  • Opening file: f = open(<path>[, mode="<modificator>"][, encoding="utf-8"])

  • Modificators:

    • r – read only (default method)

    • r+ – read and write

    • w – write (with flushing) (> in shell)

    • a – appending (>> in shell)

    • b – binary mode

Basic operations

  • Reading:

    • f.read(N) – read N bytes (into string)

    • f.readline() – read one line (with separator)

    • f.readlines() – read all file in a list

  • f.close() – closing file (Python closes it on exit)

  • Writing:

    • f.write(text) – write string to file (w/o closing)

    • f.writelines(lines) – write list of strings to file

    • f.flush() – sync buffer to disk (w/o closing file)

Changing position:

From Python 3.2: In text files (those opened without a "b" in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)).

  • f.tell() – returns the file’s current position

  • f.seek(N, [whence=0]) – shift N bytes from the beginning where whence (direction) can be:

    • 0 (from beginning)

    • 1 (from current position)

    • 2 (from the end)

Example:

Here we are using so-called "context managers" - via *with as" keywords. This allows us to not worry about closing the file descriptor after block of with-as code.

πŸͺ„ Code:

filename = "/tmp/1.txt"
# filename = r"D:\tmp.txt"

with open(filename, "w") as f:
    f.write("Test string!!!\n")
    
with open(filename) as f:
    print(f.read())
    
print(f.closed)

πŸ“Ÿ Output:

Test string!!!

True

πŸͺ„ Code:

%ls -la /tmp/1.txt
-rw-r--r-- 1 jovyan users 15 Jun 20 11:52 /tmp/1.txt
Previous10. System LibsNextSystem libraries

Last updated 2 years ago

Was this helpful?