Python Memorandum 2

I made a tool quickly with python, so a memorandum. Since the source is that of the company, remember the important points

Directory recursive

Function to get all files

Here was very helpful.

def get_all_files(directory):
    for path, dirs, files in os.walk(directory):
        for file in files:
            yield os.path.join(path, file)

Function to get all directories

Borrowed from here.

def get_all_files(directory):
    for path, dirs, files in os.walk(directory):
        yield path

Function to get all directories and files

Borrowed from here.

def get_all_files(directory):
    for path, dirs, files in os.walk(directory):
        yield path
        for file in files:
            yield os.path.join(path, file)

When to use

for file in get_all_files('/tmp/test'):
    print file

About yield

Here was very helpful. To put it simply, it seems that the contents of the process can be left without retrun. So, you can turn it in a loop or get it with next (). In other words

def test():
	yield 'a'
	yield 'b'
	yield 'c'

for i in test():
	print i

When you run

a
b
c

Is output.

File processing

Get the filename from the full path

I use this often

file_name = os.path.basename(file)

Split extension and others from full path

I wanted to see the extension

file_title, file_ext = os.path.splitext(file_name)

Open the file in UTF-8 and read all lines

You need to be careful about unicode when opening in UTF-8

f = codecs.open(file, 'r', 'utf-8')
list = f.readlines()
f.close()

Open the file in UTF-8 and write

If the file does not exist, create it. If string is unicode, it can be written in Japanese. When printing, use print (string.encode ('utf-8')). If you don't add \ n, the line will not be broken when you open it with CRLF line break.

result = codecs.open(outfile, 'a', 'utf-8')
result.write(format("string --> %s\n" % string))
result.close()

When comparing the read files, you have to unify the character code I was quite addicted to it. .. ..

String comparison

Whether any string is included

line = "1abcdefg23456789"
target_string = "1a"

if (target_string in line):

As the number of processes increased, I sometimes felt that it was Python. Python is good for quick writing. I like it.

Recommended Posts

Python memorandum
Python Memorandum 2
Python memorandum
python memorandum
python memorandum
Python memorandum
python memorandum
Python memorandum
Python basics memorandum
Python pathlib memorandum
Python memorandum (algorithm)
Python memorandum [links]
python memorandum (sequential update)
Python
Python memorandum (personal bookmark)
Python basic memorandum part 2
[Python] Iterative processing_Personal memorandum
Memorandum @ Python OR Seminar
python memorandum super basic
Effective Python Learning Memorandum Day 15 [15/100]
Cisco Memorandum _ Python config input
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
ABC memorandum [ABC163 C --managementr] (Python)
About python beginner's memorandum function
Memorandum @ Python OR Seminar: matplotlib
[Python] SQLAlchemy error avoidance memorandum
A memorandum about correlation [Python]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Memorandum @ Python OR Seminar: Pulp
Effective Python Learning Memorandum Day 13 [13/100]
A memorandum about Python mock
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Memorandum @ Python OR Seminar: Pandas
[python] Random number generation memorandum
Effective Python Learning Memorandum Day 4 [4/100]
Memorandum @ Python OR Seminar: scikit-learn
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
python parallel / asynchronous execution memorandum
Matplotlib memorandum
linux memorandum
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
ABC memorandum [ABC159 C --Maximum Volume] (Python)
jinja2 memorandum
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Django memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics