You can include multiple wildcards in your glob pattern. This example finds all the files in the current working directory that end in a .py extension and contain the word test anywhere in their filename.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
The current working directory is an invisible property that Python holds in memory at all times. There is always a current working directory, whether you're in the Python Shell, running your own Python script from the command line, or running a Python CGI script on a web server somewhere.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
This list comprehension finds all the .xml files in the current working directory, gets the size of each file (by calling the os.stat() function), and constructs a tuple of the file size and the absolute path of each file (by calling the os.path.realpath() function).〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
For instance, you might want to add /home/my_user/bin and the current workingdirectory (the directory you are in) to the PATH variable but don't want all other users on your system to have that in their PATH too.〔【出典】Gentoo Linux ◆【License】CC-BY-SA-2.5 ◆【編集】独立行政法人情報通信研究機構 〕
もし、現在の作業ディレクトリ (current working directory) について知らないのなら、ステップ1は恐らくImportErrorを出して失敗するだろう。なぜかって? なぜならPythonは、Import検索パスの中からexampleモジュールを探し出そうとするが、examplesフォルダは検索パスのどこにもないので見つけ出すことができないからだ。この問題を解決するには、次の二つのうちのどちらか一つを行えばいい:
If you don't know about the current working directory, step 1 will probably fail with an ImportError. Why? Because Python will look for the example module in the import search path, but it won't find it because the examples folder isn't one of the directories in the search path. To get past this, you can do one of two things:〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
You can have a command file in each directory, that will be loaded automatically when you start the debugger with that as your working directory.〔【出典】日英対訳文・対応付けデータ(独立行政法人情報通信研究機構)〕"_RMS3", "2575015"
To filter a list, you can include an if clause at the end of the list comprehension. The expression after the if keyword will be evaluated for each item in the list. If the expression evaluates to True, the item will be included in the output. This list comprehension looks at the list of all .py files in the current directory, and the if expression filters that list by testing whether the size of each file is greater than 6000 bytes. There are six such files, so the list comprehension returns a list of six filenames.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
In the previous section, the glob.glob() function returned a list of relative pathnames. The first example had pathnames like 'examples¥feed.xml', and the second example had even shorter relative pathnames like 'romantest1.py'. As long as you stay in the same current working directory, these relative pathnames will work for opening files or getting file metadata. But if you want to construct an absolute pathname -- i.e. one that includes all the directory names back to the root directory or drive letter -- then you'll need the os.path.realpath() function.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
Use the os.getcwd() function to get the current working directory. When you run the graphical Python Shell, the current working directory starts as the directory where the Python Shell executable is. On Windows, this depends on where you installed Python; the default directory is c:¥Python31. If you run the Python Shell from the command line, the current working directory starts as the directory you were in when you ran python3.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕