Forums

Merging files, providing results for download

Hi, first off, I'm new with python!... I don't know what I'm doing... That aside..

I have designed a 3D printed part for opening keypad doors without touching the door, I'am trying to create a site that would allow user to download the 3D(stl) files customised with their key code. To do this I'm trying to merge two stl files.

As I've never used python before I'm pretty much struggling to do this and as this is to help prevent Coronavirus transfer I want to get this running quickly! My current issue seems to be opening the files to merge, but really I'm in over my head! I wonder if anyone can help me getting it running?

**More info My efforts so far can be found here: http://thedoordoofer.pythonanywhere.com/

I'm trying to modify this code to do the merging:

    # -*- coding: utf-8 -*-

import os

numpadPath = r'C:\Users\jerome\Desktop\NumPadJB'

numPadConfig = [1, 0,
                1, 0,
                1, 1,
                1, 0,
                0, 0,
                0, 0,
                0, 0]

numPadTouch = ['6', '1',
               '7', '2',
               '8', '3',
               '9', '4',
               '0', '5',
               'C', 'X',
               'Y', 'S']

stlFilePrefix = 'NumPadJB v3_NumPadJB v3_'
stlFileSuffix = '_NumPadJB v3.stl'

partsToMerge = ['Numpad']

for (c, n) in zip(numPadConfig, numPadTouch):
    if c:
        partsToMerge.append(n + 'P')
    else:
        partsToMerge.append(n + 'NP')

mergedFileName = os.path.join(numpadPath, 'NumPad.stl')

with open(mergedFileName, 'w') as mergedFile:

    mergedFile.write('solid numpad\n')

    for p in partsToMerge:

        partFileName = stlFilePrefix + p + stlFileSuffix

        with open(partFileName, 'r') as partFile:
            lines = partFile.readlines()

        mergedFile.write(''.join(lines[1:-2]))

    mergedFile.write('endsolid\n')

**update

Current issue, when I try to load the second stl file I get file not found error:

2020-03-25 17:08:45,402: Exception on / [POST]
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/thedoordoofer/mysite/flask_app.py", line 20, in my_form_post
    with open(fileToAdd, 'r') as partFile:
FileNotFoundError: [Errno 2] No such file or directory: 'NumPadP.stl'

I have tried prefixing it with /static/ and moving the file around, also what I don't understand is that it seems to find the first file with no error!?

**Sorry, another update - another issue

I think I have the file not found by using "/home/thedoordoofer/doordoofer/static/NumPad2.stl" as the file path. (well, my script does not crash!) The script seems to run but no data is appended to the first file partial code:

numpadPath = '/home/thedoordoofer/doordoofer/static/NumPad2.stl' fileToAdd = '/home/thedoordoofer/doordoofer/static/NumPadP.stl' with open(numpadPath, 'w') as mergedFile: mergedFile.write('solid numpad\n')

with open(fileToAdd, 'r') as partFile:
    lines = partFile.readlines()

mergedFile.write(''.join(lines[1:-2]))

mergedFile.write('endsolid\n')

Thanks

I would suggest adding some debug prints so you can see what your script is doing. When you are adding a file to the mergedFile, print the filename of the file you're adding. Then you can check that that file exists and has something in it.

Thanks, I did a fair bit of work the other night, and got it 'mostly' running, thanks for the advise on using print - I'm used to doing this with other platforms - I'm guessing I just run the script form a bash console?

My current problem seems to b some sort of caching issue (might be better starting a new post), the page run's fine - just goes to some output text just now, but when you refresh the page I would expect you to see the key generated, but the old key stays, even after all sorts of refreshes,(the stl file updated is the same as the one on the page!) however if i leave it long enough then refresh it, it shows the new key any ideas?

Thanks, PaulP

hmm- is the stuff you are doing at the module level or inside of a function? if you get the output/results anew each time inside of the response function, then that should refresh every time.