форкнуто от main/python-labs
Родитель
a7e068f465
Сommit
8c3d0dda94
@ -0,0 +1 @@
|
||||
запись строки в файл
|
||||
@ -0,0 +1,359 @@
|
||||
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32
|
||||
Type "help", "copyright", "credits" or "license()" for more information.
|
||||
stroka='Автоматизированная система управления'
|
||||
stroka
|
||||
'Автоматизированная система управления'
|
||||
fff=234.5;gg='Значение температуры = '
|
||||
print(gg, fff)
|
||||
Значение температуры = 234.5
|
||||
print(gg, fff, sep='/')
|
||||
Значение температуры = /234.5
|
||||
print(gg, fff,sep='/',end='***'); print('____')
|
||||
Значение температуры = /234.5***____
|
||||
print()
|
||||
|
||||
print(""" Здесь может выводиться
|
||||
большой текст,
|
||||
занимающий несколько строк""")
|
||||
Здесь может выводиться
|
||||
большой текст,
|
||||
занимающий несколько строк
|
||||
print("Здесь может выводиться",
|
||||
"большой текст,",
|
||||
"занимающий несколько строк")
|
||||
Здесь может выводиться большой текст, занимающий несколько строк
|
||||
import sys
|
||||
sys.stdout.write('Функция write')
|
||||
Функция write13
|
||||
sys.stdout.write('Функция write')
|
||||
Функция write13
|
||||
sys.stdout.write('Функция write\n')
|
||||
Функция write
|
||||
14
|
||||
psw=input('Введите пароль:')
|
||||
Введите пароль:a
|
||||
psw
|
||||
'a'
|
||||
type(psw)
|
||||
<class 'str'>
|
||||
while True:
|
||||
znach=float(input('Задайте коэф.усиления = '))
|
||||
if znach<17.5 or znach>23.8:
|
||||
print('Ошибка!')
|
||||
else:
|
||||
break
|
||||
|
||||
SyntaxError: inconsistent use of tabs and spaces in indentation
|
||||
while True:
|
||||
znach=float(input('Задайте коэф.усиления = '))
|
||||
if znach<17.5 or znach>23.8:
|
||||
print('Ошибка!')
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
Задайте коэф.усиления = 15.4
|
||||
Ошибка!
|
||||
Задайте коэф.усиления = 21.6
|
||||
import math
|
||||
print(eval(input('введите выражение для расчета = ')))
|
||||
введите выражение для расчета = math.log10(23/(1+math.exp(-3.24)))
|
||||
1.34504378689765
|
||||
import os
|
||||
os.chdir('L:\\III курс\\А-3-23\\Korneev')
|
||||
os.getcwd()
|
||||
'L:\\III курс\\А-3-23\\Korneev'
|
||||
korneev = os.getcwd()
|
||||
print(korneev)
|
||||
L:\III курс\А-3-23\Korneev
|
||||
help(os.mkdir)
|
||||
Help on built-in function mkdir in module nt:
|
||||
|
||||
mkdir(path, mode=511, *, dir_fd=None)
|
||||
Create a directory.
|
||||
|
||||
If dir_fd is not None, it should be a file descriptor open to a directory,
|
||||
and path should be relative; path will then be relative to that directory.
|
||||
dir_fd may not be implemented on your platform.
|
||||
If it is unavailable, using it will raise a NotImplementedError.
|
||||
|
||||
The mode argument is ignored on Windows. Where it is used, the current umask
|
||||
value is first masked out.
|
||||
|
||||
os.mkdir()
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#28>", line 1, in <module>
|
||||
os.mkdir()
|
||||
TypeError: mkdir() missing required argument 'path' (pos 1)
|
||||
os.mkdir('a')
|
||||
help(os.rmdir)
|
||||
Help on built-in function rmdir in module nt:
|
||||
|
||||
rmdir(path, *, dir_fd=None)
|
||||
Remove a directory.
|
||||
|
||||
If dir_fd is not None, it should be a file descriptor open to a directory,
|
||||
and path should be relative; path will then be relative to that directory.
|
||||
dir_fd may not be implemented on your platform.
|
||||
If it is unavailable, using it will raise a NotImplementedError.
|
||||
|
||||
os.rmdir('a')
|
||||
os.listdir()
|
||||
['Lab Raboty IT', 'poas', 'shemotehnika', 'vichmetod']
|
||||
help(os.path.isdir)
|
||||
Help on function isdir in module genericpath:
|
||||
|
||||
isdir(s)
|
||||
Return true if the pathname refers to an existing directory.
|
||||
|
||||
os.path.isdir('poas')
|
||||
True
|
||||
fil=os.path.abspath("oplata.dbf")
|
||||
fil
|
||||
'L:\\III курс\\А-3-23\\Korneev\\oplata.dbf'
|
||||
fil=os.path.abspath("text.txt")
|
||||
fil
|
||||
'L:\\III курс\\А-3-23\\Korneev\\text.txt'
|
||||
drkt=os.path.dirname(fil)
|
||||
drkt
|
||||
'L:\\III курс\\А-3-23\\Korneev'
|
||||
os.path.basename(fil)
|
||||
'text.txt'
|
||||
help(os.path.split)
|
||||
Help on function split in module ntpath:
|
||||
|
||||
split(p)
|
||||
Split a pathname.
|
||||
|
||||
Return tuple (head, tail) where tail is everything after the final slash.
|
||||
Either part may be empty.
|
||||
|
||||
os.path.split(fil)
|
||||
('L:\\III курс\\А-3-23\\Korneev', 'text.txt')
|
||||
os.path.exists(fil)
|
||||
True
|
||||
os.path.exists('L:\\III курс\\А-3-23\\Korneev\\hello.txt')
|
||||
False
|
||||
os.path.isfile(fil)
|
||||
True
|
||||
fp=open(file=drkt+'\\text.txt',mode='w')
|
||||
type(fp)
|
||||
<class '_io.TextIOWrapper'>
|
||||
dir(fp)
|
||||
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']
|
||||
fp1=open(drkt+'\\zapis2.bin',mode='wb+')
|
||||
fp.close()
|
||||
fpl.close()
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#52>", line 1, in <module>
|
||||
fpl.close()
|
||||
NameError: name 'fpl' is not defined. Did you mean: 'fil'?
|
||||
fp1.close()
|
||||
sps=list(range(1,13))
|
||||
fp2=open('zapis3.txt','w')
|
||||
fp2.write(str(sps[:4])+'\n')
|
||||
13
|
||||
fp2.write(str(sps[4:8])+'\n')
|
||||
13
|
||||
fp2.write(str(sps[8:])+'\n')
|
||||
16
|
||||
fp2.close()
|
||||
sps3=[['Иванов И.',1],['Петров П.',2],['Сидоров С.',3]]
|
||||
fp3=open('zapis4.txt','w')
|
||||
for i in range(len(sps3)):
|
||||
stroka4=sps3[i][0]+' '+str(sps3[i][1])fp3.write(stroka4)
|
||||
|
||||
SyntaxError: invalid syntax
|
||||
for i in range(len(sps3)):
|
||||
stroka4=sps3[i][0]+' '+str(sps3[i][1])fp3.write(stroka4)
|
||||
|
||||
SyntaxError: invalid syntax
|
||||
for i in range(len(sps3)):
|
||||
stroka4=sps3[i][0]+' '+str(sps3[i][1])
|
||||
fp3.write(stroka4)
|
||||
|
||||
|
||||
11
|
||||
11
|
||||
12
|
||||
fp3.close()
|
||||
gh=open('zapis5.txt','w')
|
||||
for r in sps3:
|
||||
gh.write(r[0]+' '+str(r[1])+'\n')
|
||||
|
||||
|
||||
12
|
||||
12
|
||||
13
|
||||
gh.close()
|
||||
sps1=[]
|
||||
fp=open('zapis3.txt')
|
||||
for stroka in fp:
|
||||
stroka=stroka.rstrip('\n')
|
||||
stroka=stroka.replace('[','')
|
||||
stroka=stroka.replace(']','')
|
||||
sps1=sps1+stroka.split(',')
|
||||
|
||||
|
||||
fp.close()
|
||||
stroka
|
||||
'9, 10, 11, 12'
|
||||
sps
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
||||
sps1
|
||||
['1', ' 2', ' 3', ' 4', '5', ' 6', ' 7', ' 8', '9', ' 10', ' 11', ' 12']
|
||||
for stroka in fp:
|
||||
stroka=stroka.rstrip('\n')
|
||||
stroka=stroka.replace('[','')
|
||||
stroka=stroka.replace(']','')
|
||||
stroka=stroka.replace(' ','')
|
||||
sps2=stroka.split(',')
|
||||
for i in sps2:
|
||||
sps1 = sps1 + int(i)
|
||||
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#85>", line 1, in <module>
|
||||
for stroka in fp:
|
||||
ValueError: I/O operation on closed file.
|
||||
fp=open('zapis3.txt')
|
||||
for stroka in fp:
|
||||
stroka=stroka.rstrip('\n')
|
||||
stroka=stroka.replace('[','')
|
||||
stroka=stroka.replace(']','')
|
||||
stroka=stroka.replace(' ','')
|
||||
sps2=stroka.split(',')
|
||||
for i in sps2:
|
||||
sps1 = sps1 + int(i)
|
||||
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#88>", line 8, in <module>
|
||||
sps1 = sps1 + int(i)
|
||||
TypeError: can only concatenate list (not "int") to list
|
||||
for stroka in fp:
|
||||
stroka=stroka.rstrip('\n')
|
||||
stroka=stroka.replace('[','')
|
||||
stroka=stroka.replace(']','')
|
||||
stroka=stroka.replace(' ','')
|
||||
sps2=stroka.split(',')
|
||||
for i in sps2:
|
||||
sps1 = sps1 + [int(i)]
|
||||
|
||||
|
||||
sps1
|
||||
['1', ' 2', ' 3', ' 4', '5', ' 6', ' 7', ' 8', '9', ' 10', ' 11', ' 12', 5, 6, 7, 8, 9, 10, 11, 12]
|
||||
sps1=[]
|
||||
for stroka in fp:
|
||||
stroka=stroka.rstrip('\n')
|
||||
stroka=stroka.replace('[','')
|
||||
stroka=stroka.replace(']','')
|
||||
stroka=stroka.replace(' ','')
|
||||
sps2=stroka.split(',')
|
||||
for i in sps2:
|
||||
sps1 = sps1 + [int(i)]
|
||||
|
||||
|
||||
fp.close()
|
||||
sps1
|
||||
[]
|
||||
fp=open('zapis3.txt')
|
||||
for stroka in fp:
|
||||
stroka=stroka.rstrip('\n')
|
||||
stroka=stroka.replace('[','')
|
||||
stroka=stroka.replace(']','')
|
||||
stroka=stroka.replace(' ','')
|
||||
sps2=stroka.split(',')
|
||||
for i in sps2:
|
||||
sps1 = sps1 + [int(i)]
|
||||
|
||||
|
||||
sps1
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
||||
fp.close()
|
||||
sps1
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
||||
sps2
|
||||
['9', '10', '11', '12']
|
||||
sps
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
||||
fp=open('zapis3.txt')
|
||||
stroka1=fp.read(12)
|
||||
stroka2=fp.read()
|
||||
fp.close()
|
||||
stroka1
|
||||
'[1, 2, 3, 4]'
|
||||
stroka
|
||||
'9,10,11,12'
|
||||
fp=open('zapis3.txt')
|
||||
stroka3=fp.readline()
|
||||
stroka4=fp.readlines()
|
||||
fp.close()
|
||||
stroka3
|
||||
'[1, 2, 3, 4]\n'
|
||||
stroka4
|
||||
['[5, 6, 7, 8]\n', '[9, 10, 11, 12]\n']
|
||||
import pickle
|
||||
|
||||
mnoz1={'pen','book','pen','iPhone','table','book'}
|
||||
fp=open('zapis6.mnz','wb')
|
||||
pickle.dump(mnoz1,fp)
|
||||
fp.close()
|
||||
fp=open('zapis6.mnz','rb')
|
||||
mnoz2=pickle.load(fp)
|
||||
fp.close()
|
||||
mnoz1
|
||||
{'iPhone', 'pen', 'table', 'book'}
|
||||
mnoz2
|
||||
{'iPhone', 'pen', 'table', 'book'}
|
||||
mnoz1 is mnoz2
|
||||
False
|
||||
fp=open('zapis7.2ob','wb')
|
||||
pickle.dump(mnoz1,fp)
|
||||
pickle.dump(sps3,fp)
|
||||
fp.close()
|
||||
fp=open('zapis7.2ob','rb')
|
||||
obj1=pickle.load(fp)
|
||||
obj2=pickle.load(fp)
|
||||
fp.close()
|
||||
obj1
|
||||
{'iPhone', 'pen', 'table', 'book'}
|
||||
mnoz1
|
||||
{'iPhone', 'pen', 'table', 'book'}
|
||||
obj2
|
||||
[['Иванов И.', 1], ['Петров П.', 2], ['Сидоров С.', 3]]
|
||||
sps3
|
||||
[['Иванов И.', 1], ['Петров П.', 2], ['Сидоров С.', 3]]
|
||||
mnoz1 is obj1
|
||||
False
|
||||
mnoz1 == obj1
|
||||
True
|
||||
mnoz1 == mnoz2
|
||||
True
|
||||
sps3 == obj2
|
||||
True
|
||||
import sys
|
||||
vr_out=sys.stdout
|
||||
fc=open('Stroka.txt','w')
|
||||
sys.stdout=fc
|
||||
print('запись строки в файл')
|
||||
sys.stdout=vr_out
|
||||
print('запись строки на экран')
|
||||
запись строки на экран
|
||||
fc.close()
|
||||
tmp_in = sys.stdin
|
||||
fd = open("Stroka.txt", "r")
|
||||
sys.stdin = fd
|
||||
sys.stdin
|
||||
<_io.TextIOWrapper name='Stroka.txt' mode='r' encoding='cp1251'>
|
||||
while True:
|
||||
try:
|
||||
line = input ()
|
||||
print(line)
|
||||
except EOFError:
|
||||
break
|
||||
|
||||
|
||||
запись строки в файл
|
||||
fd.close()
|
||||
sys.stdin=tmp_in
|
||||
Двоичный файл не отображается.
@ -0,0 +1,3 @@
|
||||
[1, 2, 3, 4]
|
||||
[5, 6, 7, 8]
|
||||
[9, 10, 11, 12]
|
||||
@ -0,0 +1 @@
|
||||
Иванов И. 1Петров П. 2Сидоров С. 3
|
||||
Загрузка…
Ссылка в новой задаче