open txt - string conversion issue

88 views
Skip to first unread message

Daz

unread,
Feb 12, 2014, 1:09:41 PM2/12/14
to python_in...@googlegroups.com
Heya

I'm trying to open file, read string and then assign each to separate array.

I get //// /// / /// issue...

How else I can do this?

sLG=(str(open("D:\Store\Maya\Globals.txt","r").readlines()).split())

a = sLG[1]
b = sLG[2]
print a
print b
print sLG

I get this:
D:\\Store\\Maya\\Globals.txt
D:\\Store\\Maya\\Exterior.txt
["['SaveStart", 'D:\\\\Store\\\\Maya\\\\Globals.txt', 'D:\\\\Store\\\\Maya\\\\Exterior.txt', 'D:\\\\Store\\\\Maya\\\\Int.txt', 'D:\\\\Store\\\\Maya\\\\Headlights.txt', 'D:\\\\Store\\\\Maya\\\\Rearlights.txt', 'D:\\\\Store\\\\Maya\\\\wFrontRight.txt', 'D:\\\\Store\\\\Maya\\\\wFrontLeft.txt', 'D:\\\\Store\\\\Maya\\\\wRearRight.txt', 'D:\\\\Store\\\\Maya\\\\wRearLeft.txt', 'SaveEnd', "']"]
[Finished in 0.1s]

I dont understand why I get \\\\ and \\ if in my file there is only one \ :(

Justin Israel

unread,
Feb 12, 2014, 3:03:59 PM2/12/14
to python_in...@googlegroups.com
Well lets break down the steps of what you are doing in that one-liner when you read the file:

  1. # open the file for reading

  1. open("D:\Store\Maya\Globals.txt","r") 

  1. # reads the entire file at once and splits into lines, retaining the trailing newline character
    .readlines() 

  2. # You are taking a list of strings, and converting it into a string representation of a list
    str()

  3. # Then you split that string rep of a list on newlines
    .split()
So what you are doing is adding in those extra slashes because you take a string representation of a list, then split that back into another list, adding a bunch of junk characters to each path string.

What you might have wanted to do is something like this:

    sLG = list(open("D:\Store\Maya\Globals.txt","r"))

... Which will produce output like:

    ['D:\\Store\\Maya\\Globals.txt\n', ... ]

But if you want to also strip away the newline characters, you can use a list comprehension:

    sLG = [line.strip() for line in open("D:\Store\Maya\Globals.txt","r")]

... Which will produce output like:

    ['D:\\Store\\Maya\\Globals.txt', ... ]

Looping over an open file object automatically makes it read by line.

One last recommendation I have for you is to try and always use forward slashes in your string literal paths, in Python. Regardless of it being windows, Python will handle the forward slash universally, saving you from escaping issues:

    open("D:/Store/Maya/Globals.txt")

-- justin




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/8e556b2d-2067-4818-acfc-17c1e595da5c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Marcus Ottosson

unread,
Feb 12, 2014, 4:43:12 PM2/12/14
to python_in...@googlegroups.com
Alternatively, instead of forward slashes, you could use raw strings so that you don't have to rely on forward slash being interpreted as backslash.

open(r'D:\Store\Maya\Globals.txt')

Notice the 'r' in front of the string



For more options, visit https://groups.google.com/groups/opt_out.



--
Marcus Ottosson
konstr...@gmail.com

Ævar Guðmundsson

unread,
Feb 16, 2014, 7:28:37 AM2/16/14
to python_in...@googlegroups.com

 

-------[ First off { entry level, easy access }

  Opening and reading files with a one-liner in python is a cool trick to know, i.e.

x = open( os.path.join( “this” , “that” ) ).readlines()

  But can lead to a scenario where one forgets to close the file handle again, and for each time the code runs there will be a lingering file still open in memory causing various things to get offset. 

  This would be things like:

  • ·         Marginal memory prints not being available again until at reboot
  • ·         Lingering .nfs* files on network servers.
  • ·         General trash borderlining on a memory leak definition.

x.close()

  With the line above ensures the file is closed again after use.

-------[ Secondly { intermidiate, efficient }

  The mess assosciated with this is one of the primary reason why the “with” statement was added to Python, the general file object was upgraded to inlude __enter__ and __exit__ methods so when you use the with statement to iterate over a file as you open it, the __exit__ method will remember to close the file handles afterwards and x.close() is no longer needed.

 

with open( “this.file” ) as x:

  data = x.read)

 

  The line after data can be made to run further code per line and data will be left as a list for you to manipulate once the iteration is done and the file is closed.

-------[ Thirdly { may want to avoid if new in python but gives more control over operation }

 

  By utilizing streams and buffer pipes you get away with not defining a file within your code but can make it read from stdin directly.  i.e. on the command line or within a shortcut launcher:

myTool < myfile.txt

 

  You then need this inside your script, example shows a script that grabs any line containing the word “asset_token” so when batched and ran in paralell will extract all cases of that line from an entire server, sort of like grep without the crazy flags to remember:

import sys

parameter = “asset_token”

data = [ n for n in sys.stdin if parameter in n ]

 

 This method handles the file handle closing as your script exists as the stream from stdin will be discontinued and your data container will be there as data for further manipulation.

 

 

---[ References

http://effbot.org/zone/python-with-statement.htm

http://docs.python.org/2/tutorial/inputoutput.html

http://axialcorps.com/2013/09/27/dont-slurp-how-to-read-files-in-python/

 

  Hope this stuff helps and you have fun with file handles!


p.s. by using os.path as in the example above rather than manually state a path in string format you completely negate forward slash backward slash issues, so to answer your pondering "I don´t understand why I get \\\\ and \\" the answer is as simple as "Because you are not using os.path" :)

Justin Israel

unread,
Feb 16, 2014, 1:51:16 PM2/16/14
to python_in...@googlegroups.com
CPython does close the file when it gets garbage collected, so it depends how long you hold on to the references, if at all. But closing it explicitly (or by the with context) does give you precise control of when you are freeing that resource. And it is apparently not guaranteed to close during garbage collection in any other implementation of python. 

It is still good practice to close the file when you are done with the resource, but just to show the garbage collector doing it in CPython:

$ lsof -p 30956 | wc -l
10
>>> x = open("/dev/null")
$ lsof -p 30956 | wc -l
11
>>> del x
$ lsof -p 30956 | wc -l
10
>>> def file_test():
...     x = open("/dev/null")
... 
>>> file_test()
$ lsof -p 30956 | wc -l
10




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages