LoginSignup
0
3

More than 3 years have passed since last update.

Python、os操作について

Last updated at Posted at 2020-03-21

(1)前提

デスクトップにディレクトリtestを作成し、ディレクトリ内にtest.pyとtest.txtを作成。
test.txtは、”テスト完了”と記述。
ディレクトリ内にディレクトリtext2を作成し、test2.txtを作成。
test2.txtは、”テスト2完了と記述。

test
├test.py
├test.txt(”テスト完了”と記述)
└test2
 └test2.txt(”テスト2完了”と記述)

(2)pwd

カレントディレクトリを、ターミナルでpwdを入力すると以下の通り

/Users/*******/desktop/test

(3)os.getcwd()と__ file__

os.getcwd()でカレントディレクトリの絶対パスを取得し、_file_でカレントディレクトリ内からの相対パスを取得する。

import os

print(os.getcwd())
print(__file__)

実行結果は以下の通り。

/Users/*******/Desktop/test
file_test.py

(5)os.path.abspath()

_file_で取得できるのはカレントディレクトリからの相対パスだが、os.path.abspath()で絶対パスに変換できる。ディレクトリも絶対パスで取得可能。

import os

print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))

実行結果は以下の通り。

/Users/*******/Desktop/test/file_test.py
/Users/*******/Desktop/test

(6)os.path.join

os.path.joinとは、引数に与えられた二つの文字列を結合させ、一つのパスにする事ができる。
まず、ディレクトリtest内にあるtest.txtを読み込む。

with open('test.txt',encoding='utf-8') as f:
    print(f.read())

すると、以下のようになる。

テスト完了

次に、ディレクトリtest2内にあるtest2.txtを読み込む。

with open('test2/test2.txt',encoding='utf-8') as f:
    print(f.read())

すると、以下のようになる。

テスト2完了

次に、os.path.joinを使い、カレントディレクトリまでの絶対パスと、test2.txtへの相対パスを結合したものでtest2を読み込んでみる。

import os

target = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2/test2.txt')
with open(target,encoding='utf-8') as f:
    print(f.read())

とすると、以下のようになる。

テスト2完了

(7)chdir

chdir [移動先のパス]として使う。ターミナル上でカレントディレクトリを移動する際に使う。
まず、移動先のパスを作成する。

import os
target2 = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2')

設定した移動先パスを経由してカレントディレクトリを移動する。

os.chdir(target2)

念の為、カレントディレクトリを確認し移動てきているかどうかを確認する。

print(os.getcwd())

ちゃんとtestから、test2にカレントディレクトリが移動できていることが確認できた。

/Users/*********/Desktop/test/test2

test2.txtを直接読み込んでみると(

with open('test2.txt',encoding='utf-8')as f:
    print(f.read())

以下の通り、ちゃんと出力される。
(カレントディレクトリがtestの場合は、with open('test2.txt',encoding='utf-8')の部分を、with open('test2.test2.txt',encoding='utf-8')とする必要がある。)

テスト2完了

(8)まとめ

test.py
import os

print(os.getcwd())
print(__file__)
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))

print('------------------------------------')

with open('test.txt',encoding='utf-8') as f:
    print(f.read())
with open('test2/test2.txt',encoding='utf-8') as f:
    print(f.read())
target = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2/test2.txt')
with open(target,encoding='utf-8') as f:
    print(f.read())

print('------------------------------------')

target2 = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2')
os.chdir(target2)
print(os.getcwd())
with open('test2.txt',encoding='utf-8')as f:
    print(f.read())

参考にしたサイト

Pythonで実行中のファイルの場所(パス)を取得するfile

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3