본문 바로가기

수업정리/Fundamental

command 명령어 2 - tar, glob(), matplotlib

tar : 파일을 압축하고 해제할 수 있다.

tar cvf b4mid.tar *.py : 현재 모든 .py 파일들을 b4mid.tar 로 압축하기

mv b4mid.tar backup/ : 압축한 파일 옮겨주기

tar xvf b4mid.tar : 압축한 파일 해제해주기

 

cp /mount/cs4ks_2023/news_data.* . : 현재 디렉토리에 원하는 것 복사해오기

tar xvf news_data.tar : 파일 해제하기

unzip news_data.zip : 해제하기

 

파일을 읽어서 리스트에 담고 글자 수 세기

# wc_glob.py                                                                                                                    import glob
 def read_all(file_list):
     data = []
     for f in file_list:
         ifile = open(f, 'r', encoding='utf-8')
         data += ifile.read().split()
         ifile.close()
     return data
 f_list = glob.glob('news_data/*')
 all_data =read_all(f_list)
 #print(all_data)

 #add codes for counting all words
 from collections import Counter
 def counts(data, top=10):
     wc_list = Counter(data).most_common(top)
     w_list, c_list = zip(*wc_list)
     return w_list, c_list

 wlist, clist = counts(all_data)
 print(wlist)
 print(clist)

 

 

 # cc_glob.py                                                                                                                    import glob
 def read_all(file_list):
     data = []
     for f in file_list:
         ifile = open(f, 'r', encoding='utf-8')
         data += ifile.read()
         #data += ifile.read().split()
         ifile.close()
     return data
 f_list = glob.glob('news_data/*')
 all_data =read_all(f_list)
 print(all_data)

 

matplotlib

# draw_bar.py
import matplotlib.pyplot as plt

def draw_bar(w_list, c_list, figname = 'bar_of_counts.jpg'):
    plt.bar(w_list, c_list, width=0.5)
    plt.ylabel('counts')
    plt.title('# of w(i)')
    plt.savefig(figname) # save chart as a file
    plt.show() # can't see chart from linux.
 if __name__ == "__main__":
    xaxis = ['w1', 'w2', 'w3', 'w4', 'w5']
    yaxis = [1, 3, 5, 7, 9]
    draw_bar(xaxis, yaxis)

 

원격 서버에 있는 파일 다운받기

scp ug20191234@163.239.103.144:/home/ug20191044/cs4ks/bar_of_counts.jpg .

scp ug20191234@163.239.103.144:/home/ug20191044/cs4ks/draw_bar.py