如何使python程序路径独立? [英] How to make a python program path independent?

查看:189
本文介绍了如何使python程序路径独立?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从django.shortcuts导入render_to_response
从django.template导入RequestContext
来自shapes.forms import UploadForm
import os

#TODO将此转换为使用ModelForm与自定义Django FileField
#现在我们只需将上传的shapefile粘贴到项目目录中

def upload(request):
if request.method =='POST':
form = UploadForm(request.POST,request.FILES)
如果form.is_valid():
form.handle(request.FILES ['file_obj '])
os.system('python ../../../automate.py')
#form.save()#如果一个modelform
#form.cleaned_data [ 'user'] = request.user

return render_to_response('uploaded.html',RequestContext(request,{}))
else:
form = UploadForm()
return render_to_response('upload.html',RequestContext(request,{'form':form}))

Ť他的代码是os.system('python ../../automate.py)



,我的automate.py有这个

  import os 
os.system('python unzip.py -z data / Parking.zip -o data /')
os .system('python manage.py ogrinspect data / Parking.shp Parking --srid = 4326 --mapping -multi> output.txt')
filename ='output.txt'
filename1 ='maps / models.py'
search =class Parking(models.Model):
add =\\\
layer_id = models.ForeignKey(Sdr_Layer)
content = open(filename,'r')。read()
content = content.replace(search,search + add)
fp = open(filename,'w')
fp.write(content)

content1 = open(filename1,'r')。read()

search1 =layer_attribute_name = models.CharField(max_length = 100)
add1 =\\\
+ content
#print add1
#print search1 + add1
content1 = content1 + add1
print content1
fp1 = open(filename1,'w')
fp1.write(content1)
fp1.close()
fp.close ()

os.system('python manage.py syncdb')

两者都是完全不同的路径,显然他们给我错误。我想要的是两件事


  1. 我不想指定data / Parking.shp。它本身应该从上传的文件中获取名称,并在automate.py中使用它。


  2. automate.py使用maps / models.py。这显然给我一个错误,因为我在另一个路径中执行这个文件。那么如何使代码与所有这些路径错误无关。



解决方案


  1. 您的命令行中的 unzip.py 模块,直接称为python模块( import unzip 等),以获取解压缩方法的结果。你可以通过 __ file __ 参数来获取模块的目录。


  2. ,因为这个其他SO问题显示。请注意,这仅适用于命令行( python toto.py ),而不是IDLE(例如,这很好地解释了 here



from django.shortcuts import render_to_response
from django.template import RequestContext
from shapes.forms import UploadForm
import os

# TODO convert this to using ModelForm with a custom Django FileField
# For now we just stick an uploaded shapefile into a project directory

def upload(request):
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.handle(request.FILES['file_obj'])
            os.system('python ../../../automate.py') 
            #form.save() # if a modelform
            #form.cleaned_data['user'] = request.user

            return render_to_response('uploaded.html', RequestContext(request,{}))
    else:
        form = UploadForm()
    return render_to_response('upload.html', RequestContext(request,{'form': form}))

This is my code which os.system('python ../../automate.py)

and my automate.py has this

import os
os.system('python unzip.py -z data/Parking.zip -o data/')
os.system('python manage.py ogrinspect  data/Parking.shp Parking --srid=4326 --mapping --multi > output.txt')
filename='output.txt'
filename1='maps/models.py'
search="class Parking(models.Model):"
add="\n    layer_id= models.ForeignKey(Sdr_Layer)"
content=open(filename,'r').read()
content=content.replace(search,search+add)
fp=open(filename,'w')
fp.write(content)

content1=open(filename1,'r').read()

search1="layer_attribute_name = models.CharField(max_length = 100)"
add1 = "\n" + content
#print add1  
#print search1+add1
content1=content1 + add1
print content1 
fp1=open(filename1,'w')
fp1.write(content1)
fp1.close()
fp.close()

os.system('python manage.py syncdb')

Both are in completely different paths so obviously they give me errors . What I want is two things .

  1. I don't want to specify "data/Parking.shp" . It should itself get the name from the file that was uploaded and use it in automate.py .

  2. automate.py uses the maps/models.py . Which obviously gives me an error as I am executing this file in another path . So how do I make the code independent of all this path errors .

解决方案

  1. Instead of calling your unzip.py module in command line, call it directly as a python module (import unzip etc) to get the results of the unzip method. That should nedd only basic adaptations.

  2. You can get the directory of a module via its __file__ parameter, as this other SO question shows. Note that this only works in command line (python toto.py), not with IDLE for instance (this is well explained here)

这篇关于如何使python程序路径独立?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆