File operations in Python

at first

Functions used when manipulating files in Python

Get file list

python


import os
os.listdir('../') #Pass PATH as a string as an argument

Confirmation of existence of files and folders

python


os.path.exists('../test.csv') #Confirmation of existence of files and folders
os.path.isfile('../test.csv') #Check if the target is a file
os.path.isdir('../test') #Check if the target is a folder

Rename files and folders

python


os.rename('../test.csv', '../test2.csv') #Arguments are in the order of the original file PATH and the changed file PATH.

Create a folder

python


os.makedirs('../test') #Create a folder in the specified PATH(Error if the folder already exists)

copy

Copy file

python


import shutil
shutil.copy2('../test.csv', '../test/test.csv') #Arguments are in the order of copy source and copy destination

Copy folder

python


shutil.copytree('../test/', '../test2/') #Arguments are in the order of copy source folder and copy destination folder

Move

Move files

python


shutil.move('../test.csv', '../test/test.csv') #Arguments are in the order of copy source folder and copy destination

Move folder

python


shutil.copytree('../test/', '../test2/') #Arguments are in the order of copy source folder and copy destination folder
shutil.rmtree('../test/')

Delete

Delete file

python


os.remove('../test/test.csv')

Delete folder

python


shutil.rmtree('../test/')

Read

python



f = open('test.txt','r')

for row in f:
	print row.strip()

f.close()

#Or

with open('test.txt','r') as f:
    for row in f:
        print row.strip()  #close when using with()Is not needed

writing

python


f = open('test.txt','w')

f.write('hoge\n')

f.close()

Recommended Posts

File operations in Python
File operations in Python
ORC, Parquet file operations in Python
[Python] File / directory operations
File processing in Python
Summary of python file operations
Four arithmetic operations in python
Download the file in Python
Wrapping git operations in Python
File operations
File / folder path manipulation in Python
Perform Scala-like collection operations in Python
Save the binary file in Python
Linebot creation & file sharing in Python
Create a binary file in Python
Python memo ① Folder and file operations
Quadtree in Python --2
Python in optimization
CURL in python
Script python file
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Exclusive control with lock file in Python
Convert psd file to png in Python
Write O_SYNC file in C and Python