Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with temporary directory in Windows OS #70

Open
derwyddon opened this issue Apr 30, 2020 · 3 comments
Open

Problem with temporary directory in Windows OS #70

derwyddon opened this issue Apr 30, 2020 · 3 comments

Comments

@derwyddon
Copy link

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.

@chaosAD
Copy link
Contributor

chaosAD commented Apr 30, 2020

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

@derwyddon
Copy link
Author

derwyddon commented May 1, 2020

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants