Shiva
Shiva

Reputation: 1026

Python:Errno 22 Invalid argument

I am saving an attachment in a folder with python(2.7.5) and django(1.6.1).It is perfectly working on linux machine.But i am executing this project on windows i am getting following error.

[Errno 22] Invalid argument
Request Method: POST
Request URL:    http://127.0.0.1:8000/save_partner
Django Version: 1.6.1
Exception Type: OSError
Exception Value:    
[Errno 22] Invalid argument
Exception Location: C:\Python27\lib\site-packages\django\core\files\storage.py in    _save, line 199
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.6
Python Path:    
['C:\\Projects\\customer',
'C:\\Python27\\lib\\site-packages\\distribute-0.6.49-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pymongo-2.6.3-py2.7-win-amd64.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info']
Server time:    Sat, 29 Mar 2014 12:53:35 +0530    

The error is showing in default_storage(last line) line

for i in request.FILES.getlist('avtar'):
    avatarName = i.name
    filepath='avatars/'+date+'_'+avatarName
    address = settings.MEDIA_ROOT+filepath
    path = refine(address)
    avtar_path=path
    default_storage.save("%s"%(filepath), ContentFile(i.read()))

default_storage.save("%s"%(filepath), ContentFile(i.read()))

the default _storage is imported from django

from django.core.files.storage import default_storage

Why it is not working in windows7..?

Upvotes: 0

Views: 3987

Answers (1)

rubayeet
rubayeet

Reputation: 9400

How are you populating filepath? Most likely you are constructing it using Linux file path convention, Linux uses front-slash (/) as path separator whereas Windows uses back-slash (\). Use Python's os.path module to formulate file paths in a platform independent manner.

#prints spam\egg on Windows and spam/egg on Linux
print os.path.join('spam', 'egg')

Upvotes: 1

Related Questions