0

For your information, when I execute the code below for first time in my jupyter notebook, it is running with no problem:

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

But during second time, I execute the same code (want it to run the latest version of the .txt after generated the latest version of the .txt file), it is not able to run:

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

Any helpful solution will be very much appreciate! Thank you!

Tyson
  • 55
  • 15

1 Answers1

0

Just a wild guess - posting as an answer because it would not fit the comment system.

The first time you have only one file. The wildcard in the command will be shell-expanded to:

./txt2pdf.py Results/first_file.txt

The second time you have two files so the command is expanded to:

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

And you get an error because txt2pdf is not expecting several filenames (guessing from the error message, we can't really know which txt2pdf.py you are using).

If you are on a Unix-like OS you can try the sh module instead (untested):

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

Then you can do this several times:

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

You may have to !pip install sh if it is not installed yet.

If you are on Windows, go to the Windows Store and install Ubuntu. The Linux sub system for Windows actually transform a Windows machine into a decent development workstation (personally I don't like Linux desktops and use a Mac or Windows with WSSL).

If you are not willing to install Ubuntu from the Windows Store try to replace the sh module by pbs - the syntax is slight different so you must check the documentation or use this hack replacing import sh above by this (you may have to !pip install pbs or conda install pbs first):

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()
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • Yes, installing the sh. Trying it now. ImportError: sh 1.12.14 is currently only supported on linux and osx. please install pbs 0.110 (http://pypi.python.org/pypi/pbs) for windows support. – Tyson Apr 27 '18 at 06:46
  • On Windows 10 I like to install Ubuntu from the Windows Store. Other than that you can install the pbs module - check https://stackoverflow.com/questions/28618906/porting-sh-1-11-based-code-to-windows – Paulo Scardine Apr 27 '18 at 06:51
  • Thanks for your helping Paublo. But the script not run in terminal. It is need to be run in jupyter notebook cell. so this sh.python('txt2pdf.py', get_last_txt("Results")) will not be able to run in it. In order to run this code: run txt2pdf.py [Results/*.txt] [Need to specify the .txt file] – Tyson Apr 27 '18 at 06:54
  • Oh, how do you start your Jupyter notebook on Windows? I start mine from the Ubuntu shell - but check my updated answer. – Paulo Scardine Apr 27 '18 at 06:58
  • Yes, Paublo, launch it from the Anaconda Navigator. :) – Tyson Apr 27 '18 at 06:59
  • Fair enough. I hope to be covering all bases with my last update. :-) – Paulo Scardine Apr 27 '18 at 07:05
  • Yes, have check your latest edit, now import it have no problem. It will be best can turn this get_last_txt("Results") into some .txt maybe? then will be executed the code well. – Tyson Apr 27 '18 at 07:05
  • is the `sh.python('txt2pdf.py', get_last_txt("Results"))` working? If not, did you get any error? – Paulo Scardine Apr 27 '18 at 07:09
  • Thanks Paublo. It is working now with this in the jupyter notebook cell: sh.python('txt2pdf.py', get_last_txt("Results")) I can get the final output in the Results folder :) Thanks Paublo!! :) – Tyson Apr 27 '18 at 07:26
  • You may get a nicer output using `print(sh.python('txt2pdf.py', get_last_txt("Results")).stdout)` but it is just cosmetic. – Paulo Scardine Apr 27 '18 at 07:32
  • Thanks Paublo for this cosmetic tips, the not showing in the cell :) Thank you very much again :) – Tyson Apr 27 '18 at 07:48