Tips on Python file input / output

Get file name

Get all file names with path

Get and display all files under the current directory.

import os
import glob
rootpath='.'
for root, dirs, files in os.walk(rootpath):
    for file_ in files:
        file_path=os.path.join(root,file_)
        print file_path 

Get all file names without path (file name only)

Get and display all files under the current directory.

import os
import glob
rootpath='.'
for root, dirs, files in os.walk(rootpath):
    for file_ in files:
        file_path=os.path.join(root,file_).split('/')[-1]
        print file_path

Extension selection

Recursively get all .tab files under the current directory and display them.

import os
import glob
path='.'
for root, dirs, files in os.walk(path):
    for dir_ in dirs:
        dir_path=os.path.join(root,dir_)
        for table in glob.glob(dir_path+'/*.tab'):
            print table

Give a file name

Align the digits filled with 0

>>> num=1
>>> "{0,04d}".format(num)
'0001'
>>> num=1
>>> "{0,4d}".format(num)
'   1'
>>> char='1'
>>> char.zfill(4)
'0001'

Extract only numbers from the file name

>>> import re
>>> hoge='1257cfnewoaij2203'
>>> suuji=re.findall("(\d+)",hoge)
>>> print suuji
'1257' '2203'

Read file

It is assumed that each column contains a physical quantity. If the columns are separated by whitespace.

General IO

When a mixture of number strings and character strings

for line in open('test.tab', 'r'):
    itemList = line[:-1].split()
    print itemList

Split (',') if separated by,, split ('/ t') if separated by tabs.

Read by Numpy

>>> import numpy as np
>>> data=np.loadtxt('input.txt').T

Writing a file

General IO

When a mixture of number strings and character strings

f=open('output.txt','w')
f.write(moji+'\n')
f.close()

Write by Numpy

>>> np.savetxt('output.txt',data.T)

Recommended Posts

Tips on Python file input / output
Notes for Python file input / output
python input and output
Python audio input / output
Receiving standard input tips @ python
python tips
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
python tips
Python application: Data handling Part 1: Data formatting and file input / output
Python CGI file created on Windows
Python Tips
Python tips
Data input / output in Python (CSV, JSON)
Data handling 1 Data formatting and file input / output
Notes on standard input / output of Go
See file and folder information on python
[Python] Chapter 02-03 Basics of Python programs (input / output)
Python Conda Tips
Script python file
Python on Windows
twitter on python3
python learning output
Python debugging tips
[Python] Standard input
Python click tips
Tips (input / output) that you should know when programming competitive programming with Python2
Python file processing
Unexpectedly (?) Python tips
python on mac
Python on Windbg
Output python log to both console and file
2017-02-19 Python> Link> Redirect to output file immediately> sys.stdout.flush ()
Speed evaluation of CSV file output in Python
Python conda on cygwin
Install python on WSL
File operations in Python
Key input in Python
Python and numpy tips
[Python] File / directory operations
PyOpenGL setup on Python 3
[Note] File reading ~ Python ~
File processing in Python
Install Python on Pidora.
Install Scrapy on python3
Automatically execute python file
Install Python on Mac
Install Python 3 on Mac
Install Python3.4 on CentOS 6.6
Reading pyc file (Python 3.5.2)
Python Tips (my memo)
Installing pandas on python2.6
File operations in Python
[Python] Change standard input from keyboard to text file
Output python execution time
python basic on windows ②
Install python on windows
Memorize Python commentary 4 --Input
Read json file with Python, format it, and output json
Python PyTorch install tips
Install Python 2.7.3 on CentOS 5.4