Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Showing certain files in a specific subdirectory


Sometimes we want to know which files are available under a specific directory or subdirectory. Assume that we save both npv_f.py and pv_f.py at C:\Python33\. To double check their existence, we have the following code:

>>>from os import listdir
>>>listdir("c:\Python33")

Actually, we can create a function called dir2() to mimic the dir() function. The difference is that the dir() function lists variables and functions in the memory, while the dir2() function shows files in a given directory. Thus, the dir() function does not need an input, while our dir2() function needs an input value, that is, a directory, as shown in the following code:

def dir2(path):
    from os import listdir
    print(listdir(path))    

After we save dir2.py at C:\Python33\, we issue the following command to view it:

>>>from dir2 import *
>>>path='c:\python33'