import streamlit as st
import pandas as pd
import os
st.header('1. guide.csv 파일을 업로드해주세요.')
uploaded_file = st.file_uploader('', type='csv')
if uploaded_file:
st.header('guide.csv')
df = pd.read_csv(uploaded_file)
st.write(df)
# guide.csv를 업로드하면 동일 디렉토리에 바로 저장되도록 하는 코드
csv_dir = "./"
if not os.path.exists(csv_dir):
os.makedirs(csv_dir)
csv_file_path = os.path.join(csv_dir, uploaded_file.name)
with open(csv_file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
-> streamlit에서 파일업로드를 한 후 업로드한 파일을 바로 저장되도록 하기 위해 작성된 코드이다.
with open(csv_file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
아래는 chatGPT가 해석해준 결과이다.
-> 위 코드는 uploaded_file 객체를 이용하여 csv_file_path 경로에 해당 파일을 저장하는 코드입니다.
uploaded_file은 파일 업로드 위젯을 통해 업로드된 파일 객체입니다. 이 객체는 getbuffer() 메소드를 사용하여 파일 내용을 메모리에 로드할 수 있습니다.
open() 함수는 csv_file_path 경로에 해당하는 파일을 열고, "wb" 모드로 파일을 쓰기 가능한 상태로 엽니다.
그리고 write() 메소드를 사용하여 uploaded_file의 내용을 csv_file_path에 쓰는 것으로 해당 파일이 저장됩니다.
즉, uploaded_file 객체에 저장된 파일을 csv_file_path 경로에 파일로 저장하는 것입니다.
'Language&Framework > Python' 카테고리의 다른 글
[python] TypeError: Object of type datetime is not JSON serializable 에러 (0) | 2023.03.07 |
---|---|
[pandas] 파일 열기 모드(mode='w') (0) | 2023.02.28 |
[pandas] 데이터프레임의 마지막 row 삭제하기 (0) | 2023.02.24 |
[pandas] to_excel 할 때 저장할 파일의 이름이 같을 때 시트가 덮어씌워지는 문제 (0) | 2023.02.24 |
[pandas] 데이터의 마지막줄에 빈칸을 추가할 때 Warning 메세지 (0) | 2023.02.22 |