input(prompt)
python a.py < stdin.txt
python a.py > stdout.txt
python a.py >> stdout.txt
python a.py < stdin.txt > stdout.txt
f = open("filename.txt")
for line in f:
print(line)
f.close()
f = open("filename.txt")
# Whole file as string
a = []
for i in range(100):
a.append("All work and no play makes Jack a dull boy ");
f = open("anotherfile.txt", 'w')
for line in a:
f.write(line)
f.close()
file.seek()
https://docs.python.org/3/library/linecache.html
00000000 00000000 00000000 00000000 = int 0
00000000 00000000 00000000 00000001 = int 1
00000000 00000000 00000000 00000010 = int 2
00000000 00000000 00000000 00000100 = int 4
00000000 00000000 00000000 00110001 = int 49
00000000 00000000 00000000 01000001 = int 65
00000000 00000000 00000000 11111111 = int 255
00000000 01000001 = code 65 = char "A"
00000000 01100001 = code 97 = char "a"
char = chr(8) # Try 7, as well!
print("hello" + char + "world")
print("hello\nworld");
00000000 00110001 = code 49 = char "1"
00000000 00110001 = code 49 = char "1"
00000000 00110001 00000000 00110010 = code 49, 50 = char "1" "2"
00000000 00110001 00000000 00110010
00000000 00110111 = code 49, 50, 55 = char "1" "2" "7"
00000000 00000000 00000000 01111111 = int 127
f = open("anotherfile.txt", xxxx)
Character:
"r"
|
Meaning: open for reading (default) open for writing, truncating the file first open for exclusive creation, failing if the file already exists open for writing, appending to the end of the file if it exists binary mode text mode (default) open a disk file for updating (reading and writing) universal newlines mode (deprecated) |
The default mode is "r" (open for reading text, synonym of "rt"). For binary read-write access, the mode "w+b" opens and truncates the file to 0 bytes. "r+b" opens the file without truncation. |
f = open("in.txt")
data = []
for line in f:
parsed_line = str.split(line,",")
data_line = []
for word in parsed_line:
data_line.append(float(word))
data.append(data_line)
print(data)
f.close()
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
with open("data.txt") as f:
for line in f:
print(line)
with A() as a, B() as b:
with contextlib.redirect_stdout(new_target):
import fileinput
a = ["file1.txt", "file2.txt", "file3.txt", "file4.txt"]
b = fileinput.input(a)
for line in b:
print(b.filename())
print(line)
b.close()
import fileinput
a = ["file1.txt", "file2.txt", "file3.txt", "file4.txt"]
b = fileinput.input(a, inplace=1, backup='.bak')
for line in b:
print("new text")
b.close()
'''
inplace = 1 # Redirects the stout (i.e. print) to the file.
backup ='.bak' # Backs up each file to file1.txt.bak etc. before writing.
'''
print(*objects, sep='', end='\n', file=sys.stdout, flush=False)