Functions used when manipulating files in Python
python
import os
os.listdir('../') #Pass PATH as a string as an argument
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
python
os.rename('../test.csv', '../test2.csv') #Arguments are in the order of the original file PATH and the changed file PATH.
python
os.makedirs('../test') #Create a folder in the specified PATH(Error if the folder already exists)
python
import shutil
shutil.copy2('../test.csv', '../test/test.csv') #Arguments are in the order of copy source and copy destination
python
shutil.copytree('../test/', '../test2/') #Arguments are in the order of copy source folder and copy destination folder
python
shutil.move('../test.csv', '../test/test.csv') #Arguments are in the order of copy source folder and copy destination
python
shutil.copytree('../test/', '../test2/') #Arguments are in the order of copy source folder and copy destination folder
shutil.rmtree('../test/')
python
os.remove('../test/test.csv')
python
shutil.rmtree('../test/')
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
python
f = open('test.txt','w')
f.write('hoge\n')
f.close()
Recommended Posts