GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add files via upload · magusto/python@b1b2a92

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • python-file-read-write-compress
    • file-read-write-compress.py
    • hello.txt.gz
    • hello.txt
    • hey.gz
    • hey.txt
    • readme.rst
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎python-file-read-write-compress/file-read-write-compress.py‎

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os, gzip
2+
3+
def read_file(fname, compress=False):
4+
if compress:
5+
f = gzip.GzipFile(fname, 'rb')
6+
else:
7+
f = open(fname, 'rb')
8+
try:
9+
data = f.read()
10+
finally:
11+
f.close()
12+
return data
13+
14+
def write_file(data, fname, compress=True):
15+
#print(os.getcwd())
16+
17+
if compress:
18+
f = gzip.GzipFile(fname, 'wb')
19+
else:
20+
f = open(fname, 'wb')
21+
try:
22+
f.write(data)
23+
finally:
24+
f.close()
25+
26+
data = read_file('hey.txt')
27+
print(data)
28+
29+
data = read_file('hey.gz', True)
30+
print(data)
31+
32+
write_file(b'This is a hello string', 'hello.txt', False)
33+
write_file(b'This is a hello string in compress format', 'hello.txt.gz')
Collapse file

‎python-file-read-write-compress/hello.txt‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a hello string
Collapse file

‎python-file-read-write-compress/hello.txt.gz‎

Copy file name to clipboard
69 Bytes
Binary file not shown.
Collapse file

‎python-file-read-write-compress/hey.gz‎

Copy file name to clipboard
48 Bytes
Binary file not shown.
Collapse file

‎python-file-read-write-compress/hey.txt‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hey, how are you?
Collapse file

‎python-file-read-write-compress/readme.rst‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can go through the tutorial https://www.roytuts.com/how-to-read-write-and-compress-text-file-in-python/

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.