開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
初めてのPerl 第6版 (Randal L. Schwartz (著)、brian d foy (著)、Tom Phoenix (著)、近藤 嘉雪 (翻訳)、オライリージャパン)の13章(ディレクトリ操作)の13.13(練習問題)7.をPythonで考えてみる。
13.13(練習問題)7.
コード(BBEdit, Emacs)
sample290_7.py
#!/usr/bin/env python3 #-*- coding: utf-8 import os import sys if len(sys.argv) != 3: sys.exit() src = sys.argv[1] dst = sys.argv[2] if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) try: os.link(src, dst) except: print(sys.exc_info())
入出力結果(Terminal)
$ ./sample290_7.py temp.txt temp1.txt $ ls -l temp*.txt temp_dir -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp1.txt temp_dir: total 0 drwxr-xr-x 2 kamimura staff 68 Jun 3 18:04 ./ drwxr-xr-x 12 kamimura staff 408 Jun 3 18:05 ../ $ ./sample290_7.py -s temp.txt temp2.txt $ ls -l temp*.txt temp_dir -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp1.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp2.txt@ -> temp.txt temp_dir: total 0 drwxr-xr-x 2 kamimura staff 68 Jun 3 18:04 ./ drwxr-xr-x 13 kamimura staff 442 Jun 3 18:06 ../ $ ./sample290_7.py -s temp.txt temp_dir $ ls -l temp*.txt temp_dir -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp1.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp2.txt@ -> temp.txt temp_dir: total 8 drwxr-xr-x 3 kamimura staff 102 Jun 3 18:06 ./ drwxr-xr-x 13 kamimura staff 442 Jun 3 18:06 ../ lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp.txt@ -> temp.txt $ ./sample290_7.py -s temp.txt temp_dir/temp3.txt $ ls -l temp*.txt temp_dir -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp1.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp2.txt@ -> temp.txt temp_dir: total 16 drwxr-xr-x 4 kamimura staff 136 Jun 3 18:07 ./ drwxr-xr-x 13 kamimura staff 442 Jun 3 18:06 ../ lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp.txt@ -> temp.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:07 temp3.txt@ -> temp.txt $ ./sample290_7.py ./temp_dir/temp.txt . (<class 'OSError'>, OSError(62, 'Too many levels of symbolic links'), <traceback object at 0x1039d97c8>) $ ls -l temp*.txt temp_dir -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 2 kamimura staff 0 May 20 14:16 temp1.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp2.txt@ -> temp.txt temp_dir: total 16 drwxr-xr-x 4 kamimura staff 136 Jun 3 18:07 ./ drwxr-xr-x 13 kamimura staff 442 Jun 3 18:06 ../ lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp.txt@ -> temp.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:07 temp3.txt@ -> temp.txt $ ./sample290_7.py temp2.txt temp_dir $ ls -l temp*.txt temp_dir -rw-r--r-- 3 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 3 kamimura staff 0 May 20 14:16 temp1.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp2.txt@ -> temp.txt temp_dir: total 16 drwxr-xr-x 5 kamimura staff 170 Jun 3 18:07 ./ drwxr-xr-x 13 kamimura staff 442 Jun 3 18:06 ../ lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp.txt@ -> temp.txt -rw-r--r-- 3 kamimura staff 0 May 20 14:16 temp2.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:07 temp3.txt@ -> temp.txt $ ./sample290_7.py ./temp_dir/temp2.txt . (<class 'FileExistsError'>, FileExistsError(17, 'File exists'), <traceback object at 0x10ab53808>) $ ./sample290_7.py ./temp_dir/temp2.txt temp3.txt $ ls -l temp*.txt temp_dir -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp1.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp2.txt@ -> temp.txt -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp3.txt temp_dir: total 16 drwxr-xr-x 5 kamimura staff 170 Jun 3 18:07 ./ drwxr-xr-x 14 kamimura staff 476 Jun 3 18:08 ../ lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp.txt@ -> temp.txt -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp2.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:07 temp3.txt@ -> temp.txt $ ./sample290_7.py -s ./temp_dir/temp2.txt temp4.txt $ ls -l temp*.txt temp_dir -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp.txt -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp1.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp2.txt@ -> temp.txt -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp3.txt lrwxr-xr-x 1 kamimura staff 20 Jun 3 18:08 temp4.txt@ -> ./temp_dir/temp2.txt temp_dir: total 16 drwxr-xr-x 5 kamimura staff 170 Jun 3 18:07 ./ drwxr-xr-x 15 kamimura staff 510 Jun 3 18:08 ../ lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:06 temp.txt@ -> temp.txt -rw-r--r-- 4 kamimura staff 0 May 20 14:16 temp2.txt lrwxr-xr-x 1 kamimura staff 8 Jun 3 18:07 temp3.txt@ -> temp.txt $
0 コメント:
コメントを投稿