代码之家  ›  专栏  ›  技术社区  ›  unmounted

C还是其他与Ironpython核心python模块相当的.NET版本?

  •  1
  • unmounted  · 技术社区  · 16 年前

    我想编译和发布(在.NET上)一些能与Ironpython很好工作的python程序,但是我对.NET还是个新手,并且在遇到与特定的python模块相关的错误。有一个编译到低级.NET的实用程序,它工作得很好,但是在处理公共库时会出错;在解释器中运行的代码不一定要编译。例如,下面利用了基本模块 shutil , getpass 和 os . getpass.getuser() 以字符串形式返回用户名。 舒特尔 提供了一个复制函数(尽管我可以用纯Python重写该函数并开始编译),以及 操作系统 用于获取文件夹信息、制作目录和取消文件链接。我应该如何根据以下几行调整全部或部分内容,以仅使用.NET本机的库?如果有人使用Ironpython作为从python到learning.net的桥梁,那么任何相关的技巧都会受到赞赏。

    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
                """
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   gimel    16 年前

    这个 os (和) shutil ).NET中的替换项位于 System.IO namespace .

    system.io命名空间包含允许读取和写入文件和数据流的类型,以及提供基本文件和目录支持的类型。

    对于大多数文件操作,请尝试 System.IO.File class . 目录信息可通过 System.IO.Directory class .

    我不知道是本地人 os.walk 或者,尝试使用 GetDirectories 和 GetFiles 方法构造自己的目录查询器。有一个例子 递归文件处理器 在 Directory.GetDirectories(String) doc .

    检索当前登录人员的用户名的简单方法可能是 System.Environment.UserName 属性。

    一个简单的交互式Ironpython示例:

    >>> 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.
    ...
    
        2
  •  0
  •   Community Mohan Dere    9 年前

    对于 os 您可以使用的模块 nt 提供一些与 操作系统 包括 stat ,请 remove ,和 mkdir . (它还包括 environ , getcwd , chdir 和 popen )

    例如:

    » import nt
    » nt.getcwd()
    'C:\\Program Files\\Microsoft Visual Studio 10.0\\Common7\\IDE'
    

    然而,实现是不完整的,因此,不幸的是,它没有 path 或 walk . 对于这些,您可能需要使用.NET System.IO 作为 gimel suggests .

    推荐文章