Python not able to find existing file?

Hello all,

I have a whole load of files where a particular layer has to be deleted and placed into a new folder as a DXF. The odd thing is I had it working perfectly earlier however when I went back to run it I noticed the command line was full of errors saying the file could not be found, but stranger than that, a copy of all my files were made in the destination folder and the desired files with the removed layers were left in the origin folder.

So the code I used was:

import os
import rhinoscriptsyntax as rs
import Rhino

path = rs.BrowseForFolder()
saveloc = rs.BrowseForFolder()
layer = "Layer 01"
extension = ".dxf"

# Create file list
files = [f for f in os.listdir(path)]
#print(files)


for each in files:
    #path = each;
    rs.DocumentModified(False)
    rs.Command('_-Open {} _Enter'.format(each))
    if layer: rs.PurgeLayer(layer)
    file = '"' + saveloc + "\\" + each +'"'
    #rs.Command('_SelAll -Export {} _Enter'.format(path))
    rs.Command("_SelAll _-Export {} _Enter".format(file))

So it seems to me the file path is identical to the location of the file - am I missing something here?

Thanks in advance,

Sash

I think I see …Desktop\Origin in the file browser and …Desktop\Test1 in the command history.
They look different.

Ayy my apologies, I tried to make the folder names clearer for people on the forum, when I ran my test it was called test 1 and test 2 which became origin and destination, but I forgot to re-run again for them to match in the screenshot

I posted the code because there is definitely a mistake there somewhere in the code, but I haven’t got the proficiency in python to spot it yet

Though trying to rerun it again now… it seems to be working again… bit bizarre frankly, earlier we tried to run it on a dropbox folder and it crashed on the first run saying the named layer wasn’t in the layer table (though we are wondering if this was caused by the named layer being the active layer in that particular file) when we copied all the files to the desktop to see if being on local storage it would affect it but the error kept appearing even when selecting the desktop folder, the error code there was saying it couldn’t find it in the original dropbox location - but I realise that this isn’t super helpful, so I will see if we can recreate the error again in a more repeatable way… (this issue was happening on 3 different computers after initially working on mine)

Sorry for the muddle, I will get back with a clearer description

1 Like

Hm, okay I managed to recreate the issue, but it makes me wonder if there are two things going on. because this recreated the issue we ran into when working on our files

  1. the directory is wrong after specifying the folders in rs.BrowseForFolder() and for some reason it tried to look for the file in a different folder

I created a new folder called Test1 and Test2 on the Desktop and copied the test files back in and got the error:
image


I realise the most likely cause is user error in specifying the wrong folder, so I ran it twice and it seems it finds the file name in the correct directory, but it seems to try to open it from the wrong directory and fails to find it

  1. When I tried to create the test conditions to post on the forum originally, the folder name was Test1 and Test2, it was looking for the file in the correct folder but still throwing up the same error

I hope that makes a bit more sense about the issues encountered - I think the issue is arising when I try to run the script from a file that doesn’t have a layer with the name being searched for… but this seems strange as the correct file should already be open by the time it tries to look for the appropriate layer to delete?

-Sash

Just for your info … :slight_smile:
You can format Python code properly here if you write a line with 3 backticks, a space and the word “python” before copying your code.
After the code there have to be a line with the 3 backticks only.
Like this:

``` python
# Python code here …
```

Have you tried to print the directory path ?
When unsure about a string content, I usually write something like

path = # ...
print '<%s>' % path

Obviously you can format the output as you like, plenty of ways to format text in Python … :wink:

You can also use os.path.isdir() and os.path.isfile() to check if your path is OK.

1 Like

Thanks for the tips Emilio, I’m sure it’s not the last time I need to post Python scripts here.

We tried specifying the path directly as a string to try and rectify it manually at one point, but the result was the same. It was returning the correct path on earlier runs even through rs.BrowseForFolder() - so you should be able to see the commented out print(path) right after the creation of the list, but it seems after copying and pasting things in windows explorer it begins to fixate on some of the accessed folders and keeps coming back to them even when the specified folders are different

I edited your original post to have the correct codeblock mark up. (:

2 Likes