Open files with characters that FC apparently doesnt like

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Open files with characters that FC apparently doesnt like

Post by openBrain »

drmacro wrote: Wed Dec 02, 2020 10:18 pm I thought that was with read rather than open when I saw your post.

I tried it with read as well, got similar error... :(
I made a quick test using a file named 'test.txt' containing some alphabet characters each on a new line and it works OK :

Code: Select all

>>> txt = open('/tmp/test.txt','r')
>>> list = txt.read().splitlines()
>>> list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', '']
drmacro
Veteran
Posts: 8982
Joined: Sun Mar 02, 2014 4:35 pm

Re: Open files with characters that FC apparently doesnt like

Post by drmacro »

openBrain wrote: Thu Dec 03, 2020 4:49 pm
I made a quick test using a file named 'test.txt' containing some alphabet characters each on a new line and it works OK :
I did subsequent similar tests and it did work...don't know what I did yesterday that didn't...

Code: Select all

import sys
import FreeCAD as App
print(sys.version_info.major)

filepath = "/home/mac/Downloads/FreeCAD/"
list = "FC_file_list.txt"
filelist  = open(filepath+list, 'r')

for afile in filelist:
   print(afile.splitlines())
   doc = FreeCAD.openDocument(filepath + afile.splitlines()[0])
   doc.save()
   App.closeDocument(doc.Name)
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Open files with characters that FC apparently doesnt like

Post by openBrain »

drmacro wrote: Thu Dec 03, 2020 4:54 pm I did subsequent similar tests and it did work...don't know what I did yesterday that didn't...
Are you sure this works ? I find it weird because there's no 'read' and the loop always pick up the item '[0]'.
I imagine something like :

Code: Select all

import sys
import FreeCAD as App
print(sys.version_info.major)

filepath = "/home/mac/Downloads/FreeCAD/"
list = "FC_file_list.txt"
filelist  = open(filepath+list, 'r').read().splitlines()

for afile in filelist:
   print(afile)
   doc = FreeCAD.openDocument(filepath + afile)
   doc.save()
   App.closeDocument(doc.Name)
drmacro
Veteran
Posts: 8982
Joined: Sun Mar 02, 2014 4:35 pm

Re: Open files with characters that FC apparently doesnt like

Post by drmacro »

openBrain wrote: Thu Dec 03, 2020 5:03 pm ..
Are you sure this works ? I find it weird because there's no 'read' and the loop always pick up the item '[0]'.
...
:?
Umm...urr I was switching back and forth between a test python file, madly cut-n-past'n, then I did that post, all before coffee.

That's not what is in the file now... :oops:

I really should know better. :roll:
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Post Reply