【问题标题】:How to use python code to choose the latest .txt to run in jupyter notebook (only python code)如何使用python代码选择最新的.txt在jupyter notebook中运行(仅限python代码)
【发布时间】:2018-04-27 06:08:04
【问题描述】:

供您参考,当我在我的 jupyter notebook 中第一次执行以下代码时,它运行没有问题:

In  []: run txt2pdf.py Results/*.txt
Out []: Writing 'Results\A_2018_04_27_13_55.txt' with 80 characters per line and 
        60 lines per page...
        PDF document: 1 pages

但在第二次,我执行相同的代码(希望它在生成最新版本的 .txt 文件后运行最新版本的 .txt),它无法运行:

In [] : run txt2pdf.py Results/*.txt
Out []: usage: txt2pdf.py [-h] [--font FONT] [--font-size FONT_SIZE]
              [--extra-vertical-space EXTRA_VERTICAL_SPACE]
              [--kerning KERNING] [--media MEDIA]
              [--minimum-page-length MINIMUM_PAGE_LENGTH] [--landscape]
              [--margin-left MARGIN_LEFT] [--margin-right MARGIN_RIGHT]
              [--margin-top MARGIN_TOP] [--margin-bottom MARGIN_BOTTOM]
              [--output OUTPUT] [--author AUTHOR] [--title TITLE]
              [--quiet] [--subject SUBJECT] [--keywords KEYWORDS]
              [--break-on-blanks] [--encoding ENCODING] [--page-numbers]
              [--line-numbers]
              filename
txt2pdf.py: error: unrecognized arguments: Results\A_2018_04_27_13_57.txt
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2

非常感谢任何有用的解决方案!谢谢!

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    只是一个疯狂的猜测 - 作为答案发布,因为它不适合评论系统。

    第一次只有一个文件。命令中的通配符将被 shell 扩展为:

    ./txt2pdf.py Results/first_file.txt
    

    第二次你有两个文件,所以命令扩展为:

    ./txt2pdf.py Results/first_file.txt Results/second_file.txt
    

    您会收到一个错误,因为 txt2pdf 不期望有多个文件名(从错误消息中猜测,我们无法真正知道您使用的是哪个 txt2pdf.py)。

    如果您使用的是类 Unix 操作系统,则可以尝试使用 sh 模块(未经测试):

    import sh
    from glob import glob
    import os
    
    # commenting line by line
    def get_last_txt(path):
        return sorted(                         # sort
            glob(path + '/*.txt'),             # all files in the folder
            key=lambda f: os.stat(f).st_mtime  # by modified time
        )[-1]                                  # return the last one
    

    然后你可以这样做几次:

    sh.python('txt2pdf.py', get_last_txt("Results"))
    

    如果尚未安装,您可能需要!pip install sh

    如果您使用的是 Windows,请转到 Windows 应用商店并安装 Ubuntu。 Windows 的 Linux 子系统实际上将一台 Windows 机器变成了一个像样的开发工作站(我个人不喜欢 Linux 桌面并且使用带有 WSSL 的 Mac 或 Windows)。

    如果您不愿意从 Windows 商店安装 Ubuntu,请尝试将 sh 模块替换为 pbs - 语法略有不同,因此您必须检查文档或 use this hack 替换上面的 import sh(您可能有先到!pip install pbsconda install pbs):

    try:
        import sh
    except ImportError:
        # fallback: emulate the sh API with pbs
        import pbs
        class Sh(object):
            def __getattr__(self, attr):
                return pbs.Command(attr)
        sh = Sh()
    

    【讨论】:

    • 是的,正在安装 sh。现在试试。 ImportError: sh 1.12.14 目前仅在 linux 和 osx 上受支持。请安装 pbs 0.110 (pypi.python.org/pypi/pbs) 以获得 Windows 支持。
    • 在 Windows 10 上,我喜欢从 Windows Store 安装 Ubuntu。除此之外,您可以安装 pbs 模块 - 检查stackoverflow.com/questions/28618906/…
    • 感谢您对保罗的帮助。但是脚本没有在终端中运行。它需要在 jupyter notebook 单元中运行。所以这个 sh.python('txt2pdf.py', get_last_txt("Results")) 将无法在其中运行。为了运行这段代码:运行txt2pdf.py [Results/*.txt] [需要指定.txt文件]
    • 哦,如何在 Windows 上启动 Jupyter notebook?我从 Ubuntu shell 开始我的 - 但请检查我更新的答案。
    • 是的,Paublo,从 Anaconda Navigator 启动它。 :)
    猜你喜欢
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2019-02-23
    • 2018-09-15
    • 2018-07-22
    相关资源
    最近更新 更多