開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Perl
『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2)の13章(ディレクトリ操作)の13.13(練習問題)1を解いてみる。
その他参考書籍
1.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl use strict; use warnings; use 5.016; use utf8; binmode STDOUT, ':utf8'; binmode STDIN, ':utf8'; print "ディレクトリ名を入力: "; chomp(my $dir = <STDIN>); if ($dir =~ /^\s*$/) { chdir or die "can't chdir home: $!"; } else { chdir $dir or die "can't chdir to $dir: $!"; } for (glob "*") { say $_; }
入出力結果(Terminal)
$ ./sample.pl ディレクトリ名を入力: build Calibre Library Copy dart Desktop Documents Downloads Dropbox Google Drive KindleGen Library Movies Music nltk nltk_data pear perl5 Pictures Projects Public public_html Sites SkyDrive $ ./sample.pl ディレクトリ名を入力: . __pycache__ barney betty coconet.dat coconet_total.dat coconet_total_2.dat date.log date.txt distribute-0.6.34.tar.gz fred gilligan.info Gilligan: ginger.info Ginger: hello_world.pl html link_test ln.txt ln1.txt log log_file.txt lovey.info Lovey: ls.err ls.out maryann.info MaryAnn: monkeyman.info MonkeyMan: numbers Oogaboogoo perl_kamimura_blog perl_kamimura_blog.html perl_program1 professor.info Professor: result sample sample.pl sample.py sample.txt sample_folder sample_text skipper.info Skipper: some_file some_folder sortable_hash standings.db test.out test.py test.txt test.txt.out test_folder test_link thurston.info Thurston: tmp.txt tmp_folder total_bytes.dat untitled text 2.txt $ ./sample.pl ディレクトリ名を入力: abcde can't chdir to abcde: No such file or directory at ./sample.pl line 15, <STDIN> line 1. $
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 ## Copyright (C) 2013 by kamimura #-*- coding: utf-8 -*- import os, glob, re dir = input("ディレクトリ名を入力: ") if re.match(r"^\s*$", dir): for file in glob.glob(os.environ["HOME"] + os.path.sep + "*"): print(file.split(os.sep)[-1]) elif os.path.isdir(dir): for file in glob.glob(dir + os.path.sep + "*"): print(file.split(os.sep)[-1])
入出力結果(Terminal)
$ ./sample.py ディレクトリ名を入力: build Calibre Library Copy dart Desktop Documents Downloads Dropbox Google Drive KindleGen Library Movies Music nltk nltk_data pear perl5 Pictures Projects Public public_html Sites SkyDrive $ ./sample.py ディレクトリ名を入力: . __pycache__ barney betty coconet.dat coconet_total.dat coconet_total_2.dat date.log date.txt distribute-0.6.34.tar.gz fred gilligan.info Gilligan: ginger.info Ginger: hello_world.pl html link_test ln.txt ln1.txt log log_file.txt lovey.info Lovey: ls.err ls.out maryann.info MaryAnn: monkeyman.info MonkeyMan: numbers Oogaboogoo perl_kamimura_blog perl_kamimura_blog.html perl_program1 professor.info Professor: result sample sample.pl sample.py sample.txt sample_folder sample_text skipper.info Skipper: some_file some_folder sortable_hash standings.db test.out test.py test.txt test.txt.out test_folder test_link thurston.info Thurston: tmp.txt tmp_folder total_bytes.dat untitled text 2.txt $ ./sample.py ディレクトリ名を入力: abcde $
0 コメント:
コメントを投稿