cd
(change directory)のことだとして回答してみます。
例えば以下のようにすれば希望の操作が実現できると思います。(あくまでも例です。)
desktop/
│
└ test_dir/
│
├ test.py
│
└ test.txt
デスクトップにtest_dirという名前のフォルダを作り、その中にtest.py、test.txtを保存するとこの構成になります。
以降test.txtは以下とします。(import this
と打つと出てくるものです。一部省略していますがプログラムに渡したのは原文の方です。)
plain
1The Zen of Python, by Tim Peters
2
3Beautiful is better than ugly.
4Explicit is better than implicit.
5Simple is better than complex.
6Complex is ...
7... -- let's do more of those!
1. open()を使ってファイルを開く
test.py
python
1with open("test.txt") as f:
2 content = "\n".join(f.readlines())
3
4print("The content in 'test.txt' was\n\n",content)
5print("\nThere are",content.count("p"),"'p's in this sentence.")
ターミナル上での操作($
以降が入力する内容です。#
以降はコメントですから打たなくていいです。)
bash
1$ cd desktop/test_dir # ディレクトリの移動
2$ python3 test.py # 実行
3The content in 'test.txt' was
4
5 The Zen of Python, by Tim Peters
6
7Beautiful is better than ugly.
8Explicit is better than implicit.
9Simple is better than complex.
10Complex is ...
11... -- let's do more of those!
12
13
14There are 20 'p's in this sentence.
15$
2. 標準入力から受け取る
test.py
python
1from sys import stdin
2
3content = "".join(stdin.readlines())
4
5print("The content was\n\n",content)
6print("\nThere are",content.count("p"),"'p's in this sentence.")
ターミナル上での操作
bash
1$ cd desktop/test_dir # ディレクトリの移動
2$ python3 test.py < test.txt # 今回はリダイレクトで渡す
3The content was
4
5 The Zen of Python, by Tim Peters
6
7Beautiful is better than ugly.
8Explicit is better than implicit.
9Simple is better than complex.
10Complex is ...
11... -- let's do more of those!
12
13
14There are 20 'p's in this sentence.
15$
実行環境 : Python 3.6.2