views:

163

answers:

1

I would like to compile and distribute (on .net) some python programs that work well with IronPython, but I am new to .net and am getting errors related to particular python modules. There is a utility for compiling to low level .net and it works well, but I get errors when dealing with common libraries; code that runs in the interpreter does not necessarily compile. For example, the below takes advantage of the basic modules shutil, getpass, and os. getpass.getuser() returns a username as a string. shutil provides, among other things, a copy function (although that one I can rewrite in pure python and get to compile), and os is used here for getting folder info, making dirs and unlinking files. How might I adapt stuff along the following lines, in whole or any part, to use only libraries native to .net? If anyone has used IronPython as a bridge from python to learning .net any related tips are appreciated.

import shutil
import os
import getpass

uname = getpass.getuser()

folders = ["/users/"+uname+"/location", "/users/"+uname+"/other_location"]

for folder in folders:
    for root, dir, files in os.walk(folder):
        for file in files:
            file_name = os.path.join(root, file)
            time_stamp = os.stat(file_name).st_mtime
            time_dict[time_stamp] = file_name
            working_list.append(time_stamp)


def sync_up():
    for item in working_list:
        if item not in update_list:
            os.remove(item)
        else:
            shutil.copy2(item, some_other_folder)

def cp_function(target=some_folder):
    if os.path.exists(target):
        sync_up()
    else:
        try:
            os.mkdir(target)
            sync_up()
        except:
            print """error connecting
            """
+1  A: 

The os (and shutil ) replacements in .NET are in the System.IO namespace.

The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

For most of your file file operations, try the methods of the System.IO.File class. Directory information is available via the System.IO.Directory class.

I'm not aware of a native os.walk alternative, try using the GetDirectories and GetFiles methods to construct your own directory walker. There is an example RecursiveFileProcessor in the Directory.GetDirectories(String) doc.

A simple way to retrieve the user name of the person who is currently logged on, could be the System.Environment.UserName property.

A simple interactive IronPython example:

>>> import clr
>>> from System import Environment
>>> Environment.UserName
'gimel'
>>> from System import IO
>>> IO.Directory.GetCreationTimeUtc('c:/')
<System.DateTime object at 0x000000000000002B [02/07/2006 12:53:25]>
>>> IO.Directory.GetLastWriteTimeUtc('c:/')
<System.DateTime object at 0x000000000000002C [09/11/2009 08:15:32]>
>>> IO.Directory.GetDirectories('C:/').Count
24
>>> help(IO.File.Copy)
Help on built-in function Copy:

Copy(...)
    Copy(str sourceFileName, str destFileName, bool overwrite)

        Copies an existing file to a new file.
         Overwriting a file of the same name is allowed.
...
gimel