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 On 12 February 2014 20:03, Justin Israel <[email protected]> wrote: > 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 > > open("D:\Store\Maya\Globals.txt","r") > > 2. # reads the entire file at once and splits into lines, retaining > the trailing newline character > .readlines() > > 3. # You are taking a list of strings, and converting it into a string > representation of a list > str() > > 4. # 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 > > > > > On Thu, Feb 13, 2014 at 7:09 AM, Daz <[email protected]> wrote: > >> 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 \ >> :( >> >> -- >> 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 [email protected]. >> 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. >> > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2D12TSm-yyUenuJV3TNkLf2nvc2SdYjNYEqAyTVXSE8g%40mail.gmail.com > . > > For more options, visit https://groups.google.com/groups/opt_out. > -- *Marcus Ottosson* [email protected] -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCr_CYvfWrzf2y4NW9h_v0GV1XUK5u%2BZQ49UKBYLR-oig%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
