Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet150/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   146   147   148   149   150   151   152   153   ...   453
134

CHAPTER 7
Accessing the .net FrAmework
Creating the Directory Example
You’ll probably find that you need access to the user’s hard drive at some point. IronPython does pro-
vide the functionality that Python provides for working with drives, directories, and folders, but many 
developers will find using the .NET functionality easier — especially if that’s what they normally rely 
upon. Listing 7-4 shows a very simple example of gaining access to directories and files. Once you gain 
this access, you can perform any task that you’d normally perform with a directory or file. The rest of 
System.IO
works amazingly like what you’d expect from any .NET language.
lISTINg 7-4: 
 Obtaining access to directories and files
# Add the .NET Framework 2.0 to the path. 
import sys
sys.path.append(‘C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727’)
# Make clr accessible.
import clr
# Import the required assemblies.
import System 
import System.IO
# Get the starting directory.
Start = raw_input(‘Type a starting directory (such as C:\\)‘)
# Create the DirectoryInfo object.
MyDir = System.IO.DirectoryInfo(Start)
# Display a list of subdirectories.
print ‘Subdirectories:’ 
for EachDir in MyDir.GetDirectories():
print EachDir, ‘\t’,
# Display a list of files. 
print ‘\n\nFiles:’ 
for EachFile in MyDir.GetFiles():
print EachFile, ‘\t’,
# Pause after the debug session.
raw_input(‘\nPress any key to continue...’)
The code begins with the usual imports. Notice that you must import the 
System.IO
assembly to 
make this example work.
At this point, the code asks the user to provide a drive specification as a starting point. You can type 
any location on your hard drive that actually exists. The example doesn’t provide error-trapping code 
for the sake of clarity (although, you could easily add it using the techniques in the “Creating .NET 
Objects” section of this chapter).
Now that the code has a drive specification to use, it creates a 
System.IO.DirectoryInfo()
object, 
MyDir
, just as you would when using a .NET language. The code uses 
MyDir
to access the 
548592c07.indd 134
2/25/10 9:44:30 AM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   146   147   148   149   150   151   152   153   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish