Skip to main content

Programming & Automation Tips

File Handling in Python 

import os
path = "C:\\Users\\Miho\\dir" 
for root, dirs, files in os.walk(path): 
    for file in files: 
        print(file)

os.walk(path): Go through all files and folders under the path
os.path.join(root, file) : Combine root and file str.endswith(".pdf"): Return True if str ends with ".pdf"

with open("my_file", "r") as file" 
for line in file: 
    if ('string1' in line.lower()) |('string2' in line.lower()): 
        print(line)

f = open("my_file", "r") 
lines = f.read() 
f.close(): 

f = open("my_file", "r") 
lines = f.read() 
f.close(): 

with open("my_file", "r"): Open my_file as read only, "a": append, "w": write, "x": create
os.path.join(root, file) : Combine root and file str.endswith(".pdf"): Return True if str ends with ".pdf"

import csv
with open('my_csv.csv') as csvfile 
lines = csv.reader(csvfile) 
for row in lines: 

Comments

Popular posts from this blog

Tried 100 Knocks by Japan Data Scientist Society

Japan Data Science Society published a set of 100 questions, answers and programming environments for people who wanted to learn data science on GitHub. This package is provided in a form of Docker. In the Docker container, Python, R and SQL are all set up, so you can write a code and run the code freely.  I have never used Docker before, so I thought this was a good opportunity for me to learn Docker. To be honest, the setup was not the easiest thing for a beginner like me. I spent about a good full day until I start using it. Unfortunately, the contents are written in only Japanese but I would like to share the experience in setting up in the case they may provide English version in the future. Prerequisite You need to download and install: 1. Git from git-scm.com This is the only screen you really have to pay attention because if you accept the default first option, the core.autocrlf value will be set to 'true' and SQL database cannot be built properly. DO NOT f...