Django Python OSError No such file or directory but file exists

222 views
Skip to first unread message

Ronaldo Bahia

unread,
Aug 1, 2017, 7:28:19 PM8/1/17
to Django users
Hi everyone, can you help me?
Thanks in advance


Code:

I'm converting doc and docx files to pdf in the server using unoconv with LibreOffice. And I need to upload to S3 the converted file.

I can convert with success the files and I can see them in the server.

But when I try to upload the pdf, I get the error. What am I missing?

Thanks in advance

This works just fine:

import subprocess
from boto.s3.connection import S3Connection, Bucket, Key

def doc_to_pdf(user):
    '''
    Convert doc or docx to PDF.

    parameter user: is a request.user

    Usage:
        doc_to_pdf(self.request.user):
    '''

    user_cv = CandidateCV.objects.get(user=user)
    user_cv_file = str(user_cv.resume).split('/')[-1] # tem que ser PDF
    user_cv_filetype = user_cv_file.split('.')[-1]

    if not user_cv_filetype in settings.PDF_FILE_TYPE:
        # Se não for PDF
        file_in = user_cv.resume.url
        file_name = file_in.split('/')[-1]
        # download
        urllib.request.urlretrieve(file_in, file_name)
        file_out = user_cv_file.split('.')[0] + '.pdf'

        # converte para PDF
        env = os.environ.copy()
        env['HOME'] = '/tmp'
        subprocess.Popen(["unoconv","-f", "pdf", "%s" % (file_in)], env = env)

        # Define a path para salvar o documento na S3
        resume_path = 'resumes/%s/' % str(date.today())

        # key é o nome do arquivo na S3
        key = '%s%s' % (resume_path, file_out)

        # deleta o arquivo localmente
        subprocess.call("rm -f %s" % user_cv_file, shell=True)

        # Salva o novo formato no banco
        user_cv.resume = key
        user_cv.save()

This is the code in which I get the error in line: k_out.set_contents_from_filename(s3file)

def s3upload(s3file):

    # Conecta na AWS S3
    conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
    bucket_out = Bucket(conn, settings.AWS_STORAGE_BUCKET_NAME)
    k_out = Key(bucket=bucket_out, name=s3file)

    # Define a path para salvar o documento na S3
    resume_path = 'resumes/%s/' % str(date.today())

    # key é o nome do arquivo na S3
    key = '%s%s' % (resume_path, s3file)
    k_out.key = key

    # Salva na AWS S3
    k_out.set_contents_from_filename(s3file)
    k_out.make_public()

    # deleta o arquivo localmente
    subprocess.call("rm -f %s" % s3file, shell=True)

"Александр Христюхин (roboslone)"

unread,
Aug 3, 2017, 12:37:41 AM8/3/17
to django...@googlegroups.com
Hi,

Are you sure s3file contains absolute path? I can't see where s3upload is being called.

Also, you might wanna use os.remove instead of calling subprocess.
You also might wanna check out PEP-8 and Sphinx for your docstrings.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7cecdcdb-7fcf-4f4a-858a-30801fa9cf9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ronaldo Bahia

unread,
Aug 4, 2017, 10:52:09 AM8/4/17
to django...@googlegroups.com
the method is called in a def post() within a class view:

user_cv = CandidateCV.objects.get(user=request.user)
user_cv_file = str(user_cv.resume).split('/')[-1]
s3upload(user_cv_file)

The file is converted from doc to pdf in server project folder, where manage.py is.
So probably there is no absolute path.

How can I solve it?

Thanks
Ronaldo Bahia
+55 11 963 622 581

Materiais gratuitos para o RH:

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

"Александр Христюхин (roboslone)"

unread,
Aug 7, 2017, 7:17:04 AM8/7/17
to django...@googlegroups.com
Well, yeah, you're using local path basically. And you should NEVER use .split('/')[-1] to determine file basename, take a look at os.path module instead.

But that's not the point. You should try to use absolute path, I believe it would do the trick.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Ronaldo Bahia

unread,
Aug 7, 2017, 4:19:48 PM8/7/17
to django...@googlegroups.com
I don't know why but if I set the string ("cv.pdf"), it process just fine.
If I use a variable instead, it doesn't.

Here is the working code:

        # convert to PDF
        env = os.environ.copy()
        env['HOME'] = '/tmp'
        subprocess.Popen(["unoconv","-f", "pdf", "-o", "cv.pdf","%s" % (file_in)], env = env)

        # Define S3 path
        resume_path = 'resumes/%s/' % str(date.today())
        
        # key is the S3 file name
        key = '%s%s' % (resume_path, file_out)
        
        # delete local file
        subprocess.call("rm -f %s" % user_cv_file, shell=True)
        
        # update the new file format
        user_cv.resume = key
        user_cv.save()
        
        # S3 Connection
        conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
        bucket_out = Bucket(conn, settings.AWS_STORAGE_BUCKET_NAME)
        k_out = Key(bucket=bucket_out, name=user_cv.resume)

        # Upload to S3
        k_out.set_contents_from_filename('cv.pdf')
        k_out.make_public()

        # deleta o arquivo localmente
        subprocess.call("rm -f cv.pdf", shell=True)

Ronaldo Bahia
+55 11 963 622 581

Materiais gratuitos para o RH:

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Ronaldo Bahia

unread,
Aug 7, 2017, 4:59:19 PM8/7/17
to django...@googlegroups.com
Turns out unoconv takes 2 seconds to perform the file conversion.
So after the file conversion, I had to set time.sleep(3) before upload a file to S3.

And after 1 week I got this working using variables.

Thanks

Ronaldo Bahia
+55 11 963 622 581

Materiais gratuitos para o RH:

"Александр Христюхин (roboslone)"

unread,
Aug 7, 2017, 5:33:53 PM8/7/17
to django...@googlegroups.com
You can wait for subprocess to finish and not rely on time.sleep.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Ronaldo Bahia

unread,
Aug 7, 2017, 5:37:03 PM8/7/17
to django...@googlegroups.com
How can I do that?

Ronaldo Bahia
+55 11 963 622 581

Materiais gratuitos para o RH:


To post to this group, send email to django-users@googlegroups.com.

-- 
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.

-- 
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Matthew Pava

unread,
Aug 7, 2017, 5:39:14 PM8/7/17
to django...@googlegroups.com

That’s a great idea.  How do you do that programmatically?

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

 

 

-- 


You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.

To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.


To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.


For more options, visit https://groups.google.com/d/optout.

 

 

-- 


You received this message because you are subscribed to the Google Groups "Django users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.


For more options, visit https://groups.google.com/d/optout.

 

 

-- 


You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.

To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

 

-- 
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 
django-users...@googlegroups.com.
To post to this group, send email to 
django...@googlegroups.com.
Visit this group at 
https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAGz5-6iVk1zxDNsxqLJ1qg3DujT%3DSXZTwXvdokWN%2BHznET-J9g%40mail.gmail.com.
For more options, visit 
https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

François Schiettecatte

unread,
Aug 7, 2017, 5:42:24 PM8/7/17
to django...@googlegroups.com
See:

https://stackoverflow.com/questions/8953119/python-waiting-for-external-launched-process-finish

François
>>>> # deleta o arquivo localmente
>>>>
>>>> subprocess
>>>> .call("rm -f %s" % s3file, shell=True)
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
>>>> To post to this group, send email to django...@googlegroups.com.
>>>> Visit this group at https://groups.google.com/group/django-users.
>>>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7cecdcdb-7fcf-4f4a-858a-30801fa9cf9b%40googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>>> --
>>> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
>>> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6BC03BEF-B4AD-49AB-8A2A-6EDDAD0DB13F%40gmail.com.
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGz5-6gvNNWthLEFfyFicV_7voe75oWKypXP6LxsuzKLeTWzDg%40mail.gmail.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
>> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3DE439B9-5926-42DC-9862-85FF20DD66BE%40gmail.com.
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGz5-6iVk1zxDNsxqLJ1qg3DujT%3DSXZTwXvdokWN%2BHznET-J9g%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CF82D6DD-FCC0-4BE4-BB27-F5011F2C66E0%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGz5-6ghkASX2Qgam3KR5pcuUg4qAcHv3AnDQdp5RQDkvjiqvg%40mail.gmail.com.

Ronaldo Bahia

unread,
Aug 7, 2017, 5:51:21 PM8/7/17
to django...@googlegroups.com
thanks a lot:

p = subprocess.Popen(('someprog.exe', str(i))
  p.wait()

Ronaldo Bahia
+55 11 963 622 581

Materiais gratuitos para o RH:

>>>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

>>>> To post to this group, send email to django...@googlegroups.com.
>>>> Visit this group at https://groups.google.com/group/django-users.
>>>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7cecdcdb-7fcf-4f4a-858a-30801fa9cf9b%40googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>>> --
>>> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
>>> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6BC03BEF-B4AD-49AB-8A2A-6EDDAD0DB13F%40gmail.com.
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
>> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3DE439B9-5926-42DC-9862-85FF20DD66BE%40gmail.com.
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IDKZeRHtxyM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages