springer_free_books icon indicating copy to clipboard operation
springer_free_books copied to clipboard

Problem with temporary directory in Windows OS

Open derwyddon opened this issue 4 years ago • 3 comments

Inside helper.py the subfolder "./tmp" is not welcomed in my Windows 10 I have replaced the line path = create_path('tmp') with path = create_path(os.path.join(book_path, 'tmp'))

I have added also a shutil.rmtree(os.path.join(book_path, 'tmp')) at the end of the _download_book(url, book_path) function

I made the workaraound without inspecting the whole code. But it is working fine now in my Windows machine. I thinks it is also compatible with other OS's as I have used os.path.join without the os.path.sep, but I have not tested it.

derwyddon avatar Apr 30 '20 07:04 derwyddon

As far as I know, Python open() can handle both format, unless it breaks somewhere else. I am running under Windows 10 too and it works just fine. It might break if you changed the code by adding other libraries on your own which cannot handle the forward-slash path.

What was the error?

Can you try changing create_path() to the following to see if it works out fine:

def create_path(path):
    path = os.path.join('', path)
    if not os.path.exists(path):
        os.makedirs(path)
    return path

chaosAD avatar Apr 30 '20 09:04 chaosAD

The error is (for each book that needs the 'tmp'): [WinError 123] El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos: '.\tmp' that in english seems to translate as "The filename, directory name, or volume label syntax is incorrect" that is a bit generic error.

Unfortunatelly I made a mistake with the first workaround and I finished with subdirs with the name of the books with the book inside with name "-temp_file-.bak". Due I supposed book_path was only the book path without including the book name.

With this _download_book tunned function I have downloaded all the books in my Win 10 Spanish (but I have not tested it in other OS's:

def _download_book(url, book_path):
    if not os.path.exists(book_path):
        with requests.get(url, stream=True) as req:
            # path = create_path('.\tmp')
            tmp_folder = os.path.join(os.path.split(book_path)[0], 'tmp')
            path = create_path(tmp_folder)
            tmp_file = os.path.join(path, '_-_temp_file_-_.bak')
            with open(tmp_file, 'wb') as out_file:
                shutil.copyfileobj(req.raw, out_file)
                out_file.close()
            shutil.move(tmp_file, book_path)
            shutil.rmtree(tmp_folder)

I have used the os.path functions join and split to create a tmp folder inside each book genre and I removed it (each time a book is downloaded... I know is not very optimized) with rmtree from shutil

derwyddon avatar May 01 '20 16:05 derwyddon

As far as I know, Python open() can handle both format, unless it breaks somewhere else. I am running under Windows 10 too and it works just fine. It might break if you changed the code by adding other libraries on your own which cannot handle the forward-slash path.

What was the error?

Can you try changing create_path() to the following to see if it works out fine:

def create_path(path):
    path = os.path.join('', path)
    if not os.path.exists(path):
        os.makedirs(path)
    return path

I forget to comment that I tried firstly your tunned function create_path but I have found the same "[WinError 123] " than with the original one.

derwyddon avatar May 01 '20 16:05 derwyddon