반응형

아래 코드는 text.txt 파일을 복사 및 폴더에 복사, 이동 코드이다.

절대경로 사용 및 상대경로 사용

import shutil

############## 복사 #######################

# 현재 디렉터리에서 파일 복사하기 후 다른 폴더로 저장
shutil.copy('./test.txt', './copyfolder')   # test.txt 파일을 -> copyfolder 라는 폴더로 복사후 이동.

# 절대경로를 이용해 저장하기
shutil.copy('C:/Users/jay/PycharmProjects/pythonProject1/test.txt', 'C:/Users/jay/PycharmProjects/pythonProject1/copyfolder')   # 절대경로에있는 파일 -> 절대경로 폴더로 저장

# 같은 디렉터리에서 파일 사본 만들기. (파일명이 서로 같으면 오류 발생)
shutil.copy('./test.txt', './newcopyTest.txt')  # test.txt 파일의 내용물이 -> newcopyTest.txt 라는 이름으로 그대로 복사.

# 전체 디렉터리(폴더)를 전체 복사
shutil.copytree('./copyfolder', './copyfolder_copy')    # copyfolder의 모든 내용물과 함께, copyfolder_copy에 모든것이 그대로 복사.

################# 이동 ##########################

# test.txt 파일을  movetest 라는 폴더로 이동
shutil.move('./test.txt', './movetest')

반응형

 

<파일에 문자를 입력하거나 문자를 불러오거나 그림파일을 불러오는 방법은 아래 참고>

https://ansan-survivor.tistory.com/925

 

[Python] 파이썬 파일 입출력하기, 파일 불러오기, 파일 읽기 쓰기, 파일에 글 추가하기

파일을 열고 한줄씩 읽기. 현재 경로에서 abc.txt 파일을 read모드로 열고 해당 instance를 print하면 한줄씩 프린트할 수 있다. #파일 불러와서 읽기. testFile = open('.\\abc.txt','r') # 'r' read의 약자, 'rb..

ansan-survivor.tistory.com

 

반응형

+ Recent posts