Python-ファイル管理
5280 ワード
一.ファイルの操作手順
二.ファイルの読み込み操作
三.非テキストファイルの読み込み
四.2つのファイルオブジェクトを同時に開く
五.オペレーティングシステム
六.ディレクトリを巡回
七.ファイルの練習
1.練習1
2.練習2
3.オペレーティングシステムの練習1
"""
: ---> --->
"""
# f = open('/tmp/pass','a')
# content = f.read()
# print(content)
# f.write('hello')
# print(f.readable())
# print(f.writable())
# f.close()
"""
r:( )
- ,
- ,
r+:
-
- ,
w:
-write only
-
- , ,
w+:
-rw
-
- , ,
a:
-write only
-
- , ,
a+:
-rw
-
-
"""
f = open('/tmp/passwd','r+')
content = f.read()
print(content)
print(f.tell())
f.write('python')
print(f.tell())
print(f.read())
print(f.tell())
f.close()
二.ファイルの読み込み操作
f = open('/tmp/passwd','rb')
# print(f.read())
# print(f.readline())
# print(f.readlines())
#readlines(): , ,
# head -c 4 /tmp/passwd
# print(f.read(4))
# print([line.strip() for line in f.readlines()])
# print(list(map(lambda x:x.strip(),f.readlines())))
#
print(f.tell())
print(f.read(3))
print(f.tell())
"""
seek ,
seek :>0, ,<0,
seek :
0:
1:
2:
"""
f.seek(-1,2)
print(f.tell())
f.close()
三.非テキストファイルの読み込み
f1 = open('1111.jpg',mode='rb')
content = f1.read()
f1.close()
f2 = open('westos.jpg',mode='wb')
f2.write(content)
f2.close()
四.2つのファイルオブジェクトを同時に開く
"""
: , with ,
"""
#
with open('/tmp/passwd') as f1,\
open('/tmp/passwdbackup','w+') as f2:
#
f2.write(f1.read())
#
f2.seek(0)
#
print(f2.read())
五.オペレーティングシステム
import os
from os.path import exists,splitext,join
#1.
# :posix, linux , nt, windows
# print(os.name)
#2.
# info = os.uname()
# print(info)
# print(info.sysname)
# print(info.nodename)
#3.
# print(os.environ)
#4. key value
# print(os.environ.get('PATH'))
#5.
# print(os.path.isabs('/tmp/hello'))
# print(os.path.isabs('hello'))
#6.
# print(os.path.abspath('hello.png'))
# print(os.path.join('/home/kiosk','hello.png'))
#7.
# filename = '/home/kiosk/PycharmProjects/westos_python/day08/hello.png'
#
# print(os.path.basename(filename))
#
# print(os.path.dirname(filename))
#8. /
# os.mkdir('img')
# os.makedirs('img/file') #
# os.rmdir('img')
#9. /
# os.mknod('westos.txt')
# os.remove('westos.txt')
#10.
# os.rename('westos.txt','data.txt')
#11.
# print(os.path.exists('data.txt'))
#12.
# print(os.path.splitext('data.txt'))
#13.
# print(os.path.split('/tmp/hello/hello.png'))
六.ディレクトリを巡回
import os
from os.path import join
for root,dir,files in os.walk('/var/log'):
# print(root)
# print(dir)
# print(files)
for name in files:
print(join(root,name))
七.ファイルの練習
1.練習1
"""
#
# 1. ips.txt, 1200 ,
172.25.254.0/24 ip;
# 2. ips.txt ip 10 ip;
"""
import random
def create_ip_file(filename):
ip = ['172.25.254.' + str(i) for i in range(0,255)]
with open(filename,'a+') as f:
for count in range(1200):
# print(random.sample(ip,1))
f.write(random.sample(ip,1)[0] + '
')
create_ip_file('ips.txt')
def sorted_by_ip(filename,count=10):
ips_dict = dict()
with open(filename) as f:
for ip in f:
if ip in ips_dict:
ips_dict[ip] += 1
else:
ips_dict[ip] = 1
sorted_ip = sorted(ips_dict.items(),key=lambda x:x[1],reverse=True)[:count]
return sorted_ip
print(sorted_by_ip('ips.txt'))
2.練習2
"""
# _*_coding:utf-8_*_
Name:day09.py
Date:1/22/19
Author:westos-dz
"""
"""
# # 1. img, 100 , 100
(X4G5.png)
# # 2. img .png .jpg.
"""
3.オペレーティングシステムの練習1
"""
# _*_coding:utf-8_*_
Name:01_ .py
Date:1/23/19
Author:westos-dz
"""
"""
100 MAC ,MAC 6 (16 ) 01-AF-3B
01-AF-3B
01-AF-3B-xx
01-AF-3B-xx-xx
01-AF-3B-xx-xx-xx
"""
import random
import string
# print(string.hexdigits)
def gen_code(len =2 ):
num = random.sample(string.hexdigits,len) ##string.hexdigits 16
return ''.join(num)
f = open('/tmp/test1','w+') ##
for i in range(100): ##
f.write('01-AF-3B')
for j in range(i) : ##
random_str1 = gen_code()
f.write('-' + random_str1)
f.write('
') ## ,
f.close()