Skip to content

Commit 3db752f

Browse files
Fix file object treatment in docs (#130)
1 parent 8947e72 commit 3db752f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

docs/index.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ simple way of inter-process communication:
1010
1111
lock = FileLock("high_ground.txt.lock")
1212
with lock:
13-
open("high_ground.txt", "a").write("You were the chosen one.")
13+
with open("high_ground.txt", "a") as f:
14+
f.write("You were the chosen one.")
1415
1516
**Don't use** a :class:`FileLock <filelock.FileLock>` to lock the file you want to write to, instead create a separate
1617
``.lock`` file as shown above.
@@ -59,11 +60,13 @@ locks:
5960
.. code-block:: python
6061
6162
with lock:
62-
open(file_path, "a").write("Hello there!")
63+
with open(file_path, "a") as f:
64+
f.write("Hello there!")
6365
6466
lock.acquire()
6567
try:
66-
open(file_path, "a").write("General Kenobi!")
68+
with open(file_path, "a") as f:
69+
f.write("General Kenobi!")
6770
finally:
6871
lock.release()
6972
@@ -82,7 +85,8 @@ acquired within ``timeout`` seconds, a :class:`Timeout <filelock.Timeout>` excep
8285
8386
try:
8487
with lock.acquire(timeout=10):
85-
open(file_path, "a").write("I have a bad feeling about this.")
88+
with open(file_path, "a") as f:
89+
f.write("I have a bad feeling about this.")
8690
except Timeout:
8791
print("Another instance of this application currently holds the lock.")
8892
@@ -92,12 +96,14 @@ The lock objects are recursive locks, which means that once acquired, they will
9296
9397
def cite1():
9498
with lock:
95-
open(file_path, "a").write("I hate it when he does that.")
99+
with open(file_path, "a") as f:
100+
f.write("I hate it when he does that.")
96101
97102
98103
def cite2():
99104
with lock:
100-
open(file_path, "a").write("You don't want to sell me death sticks.")
105+
with open(file_path, "a") as f:
106+
f.write("You don't want to sell me death sticks.")
101107
102108
103109
# The lock is acquired here.

0 commit comments

Comments
 (0)