James Sunderland
James Sunderland

Reputation: 135

Jython not finding file when using variable to pass file name

So heres the issue guys,

I have a very simple little program that reads in some setup details from a file (to make it reuseable for other sets of data) and stores them into variables.

It then uses one of those variables to open another file that I need to write some results to, as well as various search parameters.

When passing the variable to the .open() function, it fails saying it cant find the file, but when passing the exact same information, but as a written string instead of a variable, it works.

Is this a known problem, or am I just doing something wrong?

The code(problem bit bolded)

def urlTrawl(filename):
  import urllib
  read = open(getMediaPath(filename), "rt")
  baseurl = read.readline()
  orgurl = read.readline()
  lasturlfile = read.readline()
  linksfile = read.readline()
  read.close()
  webpage = ""
  links = ""
  counter = 0
  lasturl = ""
  nexturl = ""
  url = ""
  connection = ""
  try:
    read = open(lasturlfile, "rt")
    lasturl = read.readline()
  except IOError:
    print "IOError"

  webpage = connection.read()
  connection.close()
  **file = open(linksfile, "wt")**

  file.close()
  file = open(lasturlfile, "wt")
  file.write(nexturl)
  return 1

The information being passed in

http://www.questionablecontent.net/
http://www.questionablecontent.net/view.php?comic=2480
C:\\Users\\James\\Desktop\\comics\\qclast.txt
C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt
strip\"
src=\"
\"
Pevious
Next
f=\"
\"

EDIT: removed working code, to narrow down the problem area and updated code to use a direct reference rather then a relative one.

Upvotes: 0

Views: 1486

Answers (3)

user148607
user148607

Reputation: 1

I am trying to create a script which will list the datasource name and will show the connection pool utilization(pooled connection, Free Pool Size ext.) But facing the issue when list the connection pool, if the data source name having space in between the name like "Default Datasource" then it is listing list "Default Datasource and it is not parsing the datasource name correctly to the next function.

datasource = AdminConfig.list('DataSource', AdminConfig.getid( '/Cell:' 
+ cell + '/')).splitlines()                                             
for datasourceID in datasource:                                         
        datasourceName = datasourceID.split('(')[0]                     
        print datasourceName 

Request you to help if possible drop me mail at [email protected] Regards, Bubul

Upvotes: 0

James Sunderland
James Sunderland

Reputation: 135

I found the problem in the end.

The problem was that it was reading in the \n at the end of each line in my details file, and of course the \n isn't anywhere in the website data I'm reading. Removing the last character of each read did the trick:

baseurl = baseurl[:-1]
orgurl = orgurl[:-1]
lasturlfile = lasturlfile[:-1]
linksfile = linksfile[:-1]
search1 = search1[:-1]
search2 = search2[:-1]
search3 = search3[:-1]
search4 = search4[:-1]
search5 = search5[:-1]
search6 = search6[:-1]

Upvotes: 2

jpmc26
jpmc26

Reputation: 29944

I might not be right, but I think this is what's happening.

You're saying this works fine:

file = open('C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt', "wt")

But this doesn't:

# After reading three lines
linksfile = read.readline()
file = open(linksfile, "wt")

There is a difference between these two. In the first piece of code, the double slashes are escapes. They resolve to single slashes when Python is done parsing. Like so:

>>> print 'C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt'
C:\Users\James\Desktop\comics\comiclinksqc.txt

But when you read that same text from the file, there's no parsing of the text. That means that the string stored in your variable still has double slashes.

Try this command out. I bet it fails the same way as when you read the file path in:

file = open(r'C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt', "wt")

The r stands for "raw"; it prevents Python from interpreting escape characters. If it does fail the same way, then the double slashes are your problem. To fix it, in your file, you need to remove the double slashes:

C:\Users\James\Desktop\comics\comiclinksqc.txt

This isn't a problem in CPython 2.7; I'm betting it's not in 3.x, either. CPython interprets double slashes in some manner that they are effectively a single slash (in most cases, at least). So this may be an issue specific to Jython.

If unclean paths cause errors, you might want to consider doing something to clean them up. os.path.abspath might be helpful, although I can't say if Jython's implementation works as well as CPython's:

>>> print os.path.abspath(r'C:\\Users\\James\\Desktop\\comics\\comiclinksqc.txt')
C:\Users\James\Desktop\comics\comiclinksqc.txt
>>> print os.path.abspath(r'C:/Users/James/Desktop/comics/comiclinksqc.txt')
C:\Users\James\Desktop\comics\comiclinksqc.txt

Upvotes: 0

Related Questions