Re: [Tutor] path

2019-06-30 Thread Mats Wichmann
On 6/30/19 12:01 AM, ingo wrote:
> 
> 
> On 29-6-2019 15:42, Mats Wichmann wrote:
>>
>> Most people don't use pathlib, and that's kind of sad, since it tries to
>> mitigate the kinds of questions you just asked. Kudos for trying.
> 
> In the end, it works,

Sounds good.  One suggestion - a sort of general programming suggestion
really - whenever you take user input, do some kind of validation on it
before using it.  The context may not be one where anything malicious
could happen, but still, just to catch errors.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path

2019-06-29 Thread ingo



On 29-6-2019 15:42, Mats Wichmann wrote:
> 
> Most people don't use pathlib, and that's kind of sad, since it tries to
> mitigate the kinds of questions you just asked. Kudos for trying.

In the end, it works,

Ingo

---%<--%<--%<---
# set up some default directories and files
# for starting a new project with SQLite
# using Sublime + SQLTools.
#
# /---fullpath
# |
# /--- data
# /--- db
# |+--- basename.db3
# |+--- basename.read
# /--- ddl
# /--- doc
# /--- sql
# +--- basename.sublime-project
# +--- _FOSSIL_

import pathlib
import sys

# the last bit of the full path is used as the name for the database
# c:\newdatabase\test will create the databse
# c:\newdatabase\test\db\test.db3
print('enter full path for new db:')
fp = pathlib.Path(input())
fn = fp.name  # = os.path.basename()

dirs = {}
for sub in ['', 'data', 'db', 'ddl', 'doc', 'sql']:
dirs[sub] = fp / sub
try:
dirs[sub].mkdir(parents=False, exist_ok=False)
except FileExistsError:
print(f'Directory already exists: {dirs[sub]}')
sys.exit(1)

fdb = dirs['db'] / (fn+'.db3')
fdb.touch()

fr = dirs['db'] / (fn+'.read')
fr.write_text(f"""
-- template to build db from tables etc
-- using dot commands

PRAGMA foreign_keys = OFF;
--DROP TABLE IF EXISTS sometable;

BEGIN TRANSACTION;
--.read {(dirs['ddl'] / 'someddl.ddl').as_posix()}
COMMIT;

PRAGMA temp_store   = 2;
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;

BEGIN TRANSACTION;
--.read {(dirs['sql'] / 'somequery.sql').as_posix()}
COMMIT;
"""
)

fsub = dirs[''] / (fn+'.sublime-project')
fsub.write_text(f'''
{{
  "folders":[
{{
  "path": "."
}}
  ],
  "connections":{{
"Connection SQLite":{{
  "database": "{fdb.as_posix()}",
  "encoding": "utf-8",
  "type": "sqlite"
}}
  }},
  "default": "Connection SQLite"
}}'''
)

# TODO set up fossil in the fp dir


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path

2019-06-29 Thread ingo



On 29-6-2019 16:33, ingo wrote:

> 
> What I'm looking for is c:/test/this/path


After further testing, the other tools in the chain accept paths like
c:\\test\\dir
c:\/test/dir
c:/test/dir

anything except standard windows, the top two I can generate.

Ingo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path

2019-06-29 Thread ingo
On 29-6-2019 15:52, Mats Wichmann wrote:
> Sigh... something dropped my raw string, so that was a really bad sample :(
> 
> inp = r"c:\test\drive\this"
> 
> 
> On Sat, Jun 29, 2019, at 07:44, Mats Wichmann wrote:
>>
>> For your example, when you define inp as a string, it needs to be a raw
>> string because otherwise Python will interpret the backslash sequences.
>> \t means tab, which is why the the results look mangled.
>>
>> inp = "c:\test\drive\this"
>>


import pathlib
print('input')
a=input()
b=r"{}".format(a) #does this make sense to create a r'string'?
wa = pathlib.PureWindowsPath(a)
wb = pathlib.PureWindowsPath(b)
pa = pathlib.PurePosixPath(a)
pb = pathlib.PurePosixPath(b)
ppa = pathlib.PurePosixPath(wa)
ppb = pathlib.PurePosixPath(wb)


input
c:\test\this\path\
>>> print(a)
c:\test\this\path\
>>> print(b)
c:\test\this\path\
>>> print(wa)
c:\test\this\path
>>> print(wb)
c:\test\this\path
>>> print(pa)
c:\test\this\path\
>>> print(pb)
c:\test\this\path\
>>> print(ppa)
c:\/test/this/path
>>> print(ppb)
c:\/test/this/path

What I'm looking for is c:/test/this/path
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path

2019-06-29 Thread Mats Wichmann
Sigh... something dropped my raw string, so that was a really bad sample :(

inp = r"c:\test\drive\this"


On Sat, Jun 29, 2019, at 07:44, Mats Wichmann wrote:
> 
> For your example, when you define inp as a string, it needs to be a raw
> string because otherwise Python will interpret the backslash sequences.
> \t means tab, which is why the the results look mangled.
> 
> inp = "c:\test\drive\this"
> 
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path

2019-06-29 Thread Mats Wichmann
On 6/29/19 6:46 AM, ingo wrote:
> A user has to type a path in the commandline on Win 10, so just a
> string.
> A short excerpt:
> 
> Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64
> bit (AMD64)] on win32
> Type "copyright", "credits" or "license()" for more information.
 inp = "c:\test\drive\this"
 import pathlib
 p=pathlib.PurePath(inp)
 p
> PureWindowsPath('c:\test/drive\this')
 print(pathlib.PurePosixPath(p))
> c:/ est/drive his
 inp
> 'c:\test\\drive\this'
 import os
 print(os.path.normpath(inp))
> c:  est\drive his
 print(pathlib.Path(inp))
> c:  est\drive his

> 
> how to go from a string to a path, how to append to a path (os.path.join
> or / with Path), how to turn it into 'posix'

Most people don't use pathlib, and that's kind of sad, since it tries to
mitigate the kinds of questions you just asked. Kudos for trying.

For your example, when you define inp as a string, it needs to be a raw
string because otherwise Python will interpret the backslash sequences.
\t means tab, which is why the the results look mangled.

inp = "c:\test\drive\this"

If you're going to use pathlib, then may as well use the / operator for
joining,

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] path

2019-06-29 Thread ingo
A user has to type a path in the commandline on Win 10, so just a
string. This has to become a path / directory in the file system. Later
in the process subdirectories and files are 'appended' by the script. In
these files there are also paths, derived from the input path and they
have to use forward slashes. The tool that uses these files requires that.

# create structure:
# /---fullpath  <-- input() cmd
# |
# /--- data
# /--- db
# |+--- basename.db3<-- pathlib.Path(...).touch()
# |+--- basename.read   <-- forward slashes inside
# /--- ddl
# /--- doc
# /--- sql
# +--- basename.sublime-project <-- forward slashes inside (json)
# +--- _FOSSIL_


And here the misery begins...

A short excerpt:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64
bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> inp = "c:\test\drive\this"
>>> import pathlib
>>> p=pathlib.PurePath(inp)
>>> p
PureWindowsPath('c:\test/drive\this')
>>> print(pathlib.PurePosixPath(p))
c:/ est/drive his
>>> inp
'c:\test\\drive\this'
>>> import os
>>> print(os.path.normpath(inp))
c:  est\drive his
>>> print(pathlib.Path(inp))
c:  est\drive his
>>>

how to go from a string to a path, how to append to a path (os.path.join
or / with Path), how to turn it into 'posix'

TIA,

Ingo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-17 Thread Michael C
Ok, I will work with all these. Thx all!

On Aug 16, 2017 20:22, "Abdur-Rahmaan Janhangeer" 
wrote:

> in addition to the answers i'd say now you have the motivation to learn
> python data structures and algorithms
>
> http://interactivepython.org/runestone/static/pythonds/index.html
>
> barnum and miller
>
> it is free though i have not found a good pdf book form from where to
> download, but you have the site anyway !
>
> Now, the website has more materials than when i first knew it.
>
> hope it helps !
>
> Abdur-Rahmaan Janhangeer,
> Mauritius
> abdurrahmaanjanhangeer.wordpress.com
>
> On 14 Aug 2017 02:28, "Michael C"  wrote:
>
> Hi all:
>
> I am trying to formulate a "path-finding" function, and I am stuck on this
> problem:
>
> Please look at the picture attached: Those dots are coordinates of (x,y),
> and this tree can be thought of as a list of tuples, with each tuple
> consisting of (x,y).  Now I am trying to make a function go through this
> list of tuples and then return the "path." to go from, say, 4 to 8. If I
> simply compute for the dot for shortest distance, then the solution would
> be to go from 4 to 8 direct, but that doesn't work, because the correct
> solution should have been 4,3,2,5,6,8.
>
>
> How do I do this?
>
> Thanks!
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-17 Thread Abdur-Rahmaan Janhangeer
in addition to the answers i'd say now you have the motivation to learn
python data structures and algorithms

http://interactivepython.org/runestone/static/pythonds/index.html

barnum and miller

it is free though i have not found a good pdf book form from where to
download, but you have the site anyway !

Now, the website has more materials than when i first knew it.

hope it helps !

Abdur-Rahmaan Janhangeer,
Mauritius
abdurrahmaanjanhangeer.wordpress.com

On 14 Aug 2017 02:28, "Michael C"  wrote:

Hi all:

I am trying to formulate a "path-finding" function, and I am stuck on this
problem:

Please look at the picture attached: Those dots are coordinates of (x,y),
and this tree can be thought of as a list of tuples, with each tuple
consisting of (x,y).  Now I am trying to make a function go through this
list of tuples and then return the "path." to go from, say, 4 to 8. If I
simply compute for the dot for shortest distance, then the solution would
be to go from 4 to 8 direct, but that doesn't work, because the correct
solution should have been 4,3,2,5,6,8.


How do I do this?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-16 Thread Cameron Simpson

On 16Aug2017 10:22, Alan Gauld  wrote:

On 16/08/17 02:02, Cameron Simpson wrote:

Ok. So you have a graph like this:



  1 -- 2 -- 3 -- 4
   |
  7 -- 5 -- 6 -- 8

  graph = {
1: [2],
2: [1, 3],


 2: [1, 3, 5],


3: [2, 4],
4: [3],
5: [7, 6],


 5: [2, 6, 7],


6: [5, 8],
7: [5],
8: [6]
  }


The missing link is pretty critical in this case :-)


Hmm, yes. Thanks!

Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-16 Thread Alan Gauld via Tutor
On 16/08/17 02:02, Cameron Simpson wrote:

> Ok. So you have a graph like this:

>   1 -- 2 -- 3 -- 4
>|
>   7 -- 5 -- 6 -- 8
> 
>   graph = {
> 1: [2],
> 2: [1, 3],

  2: [1, 3, 5],

> 3: [2, 4],
> 4: [3],
> 5: [7, 6],

  5: [2, 6, 7],

> 6: [5, 8],
> 7: [5],
> 8: [6]
>   }

The missing link is pretty critical in this case :-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-15 Thread Cameron Simpson

On 14Aug2017 12:10, Michael C  wrote:

http://imgur.com/a/CwA2G


Ok. So you have a graph like this:

 1 -- 2 -- 3 -- 4
  |
 7 -- 5 -- 6 -- 8

Have a read of a graph theory textbook. Also, wikipedia has an article on 
finding the shortest path through a graph:


 https://en.wikipedia.org/wiki/Shortest_path_problem

which references several algorithms.

You could pick one (eg Dijkstra's algorithm) and try to implement it. For a 
graph this small you could try your own and do something rather brute force; in 
larger graphs efficiency becomes very important.


You will need to express the graph as a data structure in your code, with a 
data structure that expresses which nodes connect to each other node. Typically 
this often includes weights for the edges and a direction, but your graph has 
no weights (cost of traversing a particular edge) and is undirected (you can 
traverse an edge in either direction). It is also "simply connected" - there 
are no loops. All these things make your task simpler.


You can express a graph as a direcionary with keys being your node numbers 
(i.e. 1, 2, 3 etc) and the values being a list of the other nodes to which each 
connects. Eg:


 graph = {
   1: [2],
   2: [1, 3],
   3: [2, 4],
   4: [3],
   5: [7, 6],
   6: [5, 8],
   7: [5],
   8: [6]
 }

The you need to write code that starts somewhere (4 in your example) and moves 
to other nodes until it reaches the target node (8 in your example). You can 
see which other nodes are reachable your current from the dictionary above. You 
need to keep some kind of record of which nodes you have visted (i.e. that is 
the path).


See if that gets you started. For debugging, make your program print out what 
node it is at as it traverses the graph - that will be helpful to you in 
figuring out what is working and what is not.


Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-15 Thread Peter Otten
Martin A. Brown wrote:

> The image:
> 
>> http://imgur.com/a/CwA2G
> 
> To me, this looks like a 'graph', which is a more general data
> structure -- it does not look like a 'tree' (in the computer-science
> meaning of the term, anyway).

> import networkx as nx

While Martin's solution is certainly more robust it may also be instructive 
to see it done "by hand":

edges = [
(1, 2),
(2, 5),
(2, 3),
(3, 4),
(5, 7),
(5, 6),
(6, 8),
# remove the comment to see what happens 
# when there is more than one path between two nodes
#(1, 8), 
]

graph = {}

# make a lookup table node --> neighbours
for a, b in edges:
graph.setdefault(a, []).append(b)
graph.setdefault(b, []).append(a)
print(graph)

def connect(start, end, path, graph):
path += (start,)
if start == end:
# we found a connection
yield path
neighbours = graph[start]

# try all neighbours, but avoid cycles to nodes already in the path
for node in neighbours:
if node not in path:
# recurse to find connection from neigbour to end
yield from connect(node, end, path, graph)

for path in connect(4, 8, (), graph):
print(path)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-14 Thread Michael C
http://imgur.com/a/CwA2G

I don't know to do this with math :(








On Sun, Aug 13, 2017 at 1:07 PM, Michael C 
wrote:

> Hi all:
>
> I am trying to formulate a "path-finding" function, and I am stuck on this
> problem:
>
> Please look at the picture attached: Those dots are coordinates of (x,y),
> and this tree can be thought of as a list of tuples, with each tuple
> consisting of (x,y).  Now I am trying to make a function go through this
> list of tuples and then return the "path." to go from, say, 4 to 8. If I
> simply compute for the dot for shortest distance, then the solution would
> be to go from 4 to 8 direct, but that doesn't work, because the correct
> solution should have been 4,3,2,5,6,8.
>
>
> How do I do this?
>
> Thanks!
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-14 Thread Alan Gauld via Tutor
On 13/08/17 21:07, Michael C wrote:

> Please look at the picture attached: 

This is a text mailing list, no binary attachments allowed.
The server strips them off.

You need to put it on a web site and provide a link.


> consisting of (x,y).  Now I am trying to make a function go through this
> list of tuples and then return the "path." to go from, say, 4 to 8.

> How do I do this?

Do you know how to do it mathematically - eg with pen and paper?
If so its a matter of transcribing the algorithm into python
code. But if you don't know the background math, that's
where you need to start. Find the algorithm first.



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Path tree"

2017-08-14 Thread Mats Wichmann
On 08/13/2017 02:07 PM, Michael C wrote:
> Hi all:
> 
> I am trying to formulate a "path-finding" function, and I am stuck on this
> problem:
> 
> Please look at the picture attached: Those dots are coordinates of (x,y),
> and this tree can be thought of as a list of tuples, with each tuple
> consisting of (x,y).  Now I am trying to make a function go through this
> list of tuples and then return the "path." to go from, say, 4 to 8. If I
> simply compute for the dot for shortest distance, then the solution would
> be to go from 4 to 8 direct, but that doesn't work, because the correct
> solution should have been 4,3,2,5,6,8.
>
> How do I do this?

There is no picture, don't know if you forgot to attach, or if it got
stripped by the mailing list software (the latter does happen, although
some seem to get through).

There is quite some on path-walking solvers in Python if you search a
bit, although when I looked just now it was not as easy to find useful
stuff as I remembered from some years ago when I was interested in such
a problem.

Usually, the tutors are better able to help if you post some initial
code and explain what you're trying to do and what is going wrong or
what you don't understand.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] "Path tree"

2017-08-13 Thread Michael C
Hi all:

I am trying to formulate a "path-finding" function, and I am stuck on this
problem:

Please look at the picture attached: Those dots are coordinates of (x,y),
and this tree can be thought of as a list of tuples, with each tuple
consisting of (x,y).  Now I am trying to make a function go through this
list of tuples and then return the "path." to go from, say, 4 to 8. If I
simply compute for the dot for shortest distance, then the solution would
be to go from 4 to 8 direct, but that doesn't work, because the correct
solution should have been 4,3,2,5,6,8.


How do I do this?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path string

2017-01-02 Thread Alan Gauld via Tutor
On 02/01/17 17:01, anatta anatta wrote:

> I am trying to create unsuccessfully source path as 
> a string 'str7' in part_1 of the code below,

When you say unsuccessfully what do you mean?
What do you expect? What do you get?

> to be used in part_2 of the code.

For that you need to expose it outside the
function, the best way to do that is to return
it as a value, which you comment suggests you
want to do. But the only return is commented out,
so you need to tidy that up.

But personally I think your function is trying to do
too much. You should simplify it to only return the
files and have another function that returns the path.
Functions that try to do too many things (ie more
than one) are notoriously difficult to debug.

> When I define the source path explicitly in part_2 
> of the code (#sourcePath = r'H://TCVFLDAT'), the
> code works right.

I'll take your word for it.

> How else could I find the path in part-1 and use it in part 2?

Return the value (assuming it is the right value)
and in part two assign the return from the
function to a variable.

For my analysis below I've removed all the flag
nonsense which is just cluttering things up for
now and gone with the ENDS_WITH option as
default

> def fetchFiles(pathToFolder, flag, keyWord):
>   
>   _pathToFiles = []
>   _fileNames = []
> 
>   for dirPath, dirNames, fileNames in os.walk(pathToFolder):
>   selectedPath = [os.path.join(dirPath,item) for item in 
> fileNames if item.endswith(keyWord)]
>   _pathToFiles.extend(selectedPath)
>   
>   selectedFile = [item for item in fileNames if 
> item.endswith(keyWord)]
>   _fileNames.extend(selectedFile)

You could simplify that by putting the selectedFiles
line before the selectedPath line and use selectedFiles
inside the comprehension.

>   
>   # Try to remove empty entries if none of the required files are 
> in directory
>   try:
>   _pathToFiles.remove('')
>   _imageFiles.remove('')

It would probably be better to check if they were empty
before putting them in. Since you use the endswith() test
I'm thinking there should never be any empty ones in
this scenario anyway?

>   except ValueError:
>   pass
>   

> #return _pathToFiles, _fileNames

Here is the missing return statement but it's not returning
what you said you wanted, ie str7


> #print _pathToFiles, _fileNames
> print 'path to first tuple file is:', _pathToFiles [0]
> str1 = ' '.join(_pathToFiles [0]) #convert tuple element 0 to string
> print 'length of str1 is: ', len (str1)

It might be wise to print the string itself to check
you have what you want, I'm not sure you do... But
I'm not really sure what you want since your code
logic is confusing me a bit here.


> str2 = str1.replace(" ", "") #remove white spaces

So why did you add it above? Why not just use an empty
string in the join?

However, more seriously, what affect does this have
on any paths/filenames that you found with spaces
in them? Is that really what you want?

> print 'str2 is', str2
> str3 = str2[13:16] #extract rgeistration
> print 'str3 is registration:', str3

I'll assume this is right since I've no idea what
format you think your filenames have. However, in general,
relying on fixed positions within a string is not a good
idea. This might be a valid case for using a regex which
can more flexibly match your pattern. But for now just
stick with the simple fixed values...


> str4 = 'FLDAT'
> print 'str4 is: ', str4
> str5 = str3.__add__(str4)

You shouldn't really call the dunder methods directly you
should use the + operator:

str5 = str3 + str4

Or, in this case, save a variable and use the literal:

str5 = str3 + 'FLDAT'


> print 'str 5 is: ',str5
> str6 = 'H://'
> print 'str6 is: ', str5

Did you really mean that? You've already printed str5.
And do you really need a double slash after the
drive letter? That's usually only needed if using
backslashes ('H:\\').

> str7 = str6.__add__(str5)

Again you could just use the literals:

str7 = 'H://' + str3 = 'FLDAT'

> print 'str7 is: ', str7  
> 
> fetchFiles('H://','ENDS_WITH','.FLD')

No assignment of any return value here

>  part_2  copying files from sourcePath to destPath
> 

> sourcePath = r'str7'

This assigns the literal string 'str7' is that what you want?
You cannot access the variable str7 that was inside the function.
It was a local variable and will have been destroyed by now.

> print 'Source path is: ', sourcePath
> destPath = r'c://test_o/'
> print 'Destination path is: ', destPath
> for root, dirs, files in os.walk(sourcePath):
> 
> #figure out where we're going

[Tutor] path string

2017-01-02 Thread anatta anatta
Dear Tutor.

I am trying to create unsuccessfully source path as a string 'str7' in part_1 
of the code below, to be used in part_2 of the code.
When I define the source path explicitly in part_2 of the code (#sourcePath = 
r'H://TCVFLDAT'), the code works right.
How else could I find the path in part-1 and use it in part 2?

##
Here is my code:
##


# -*- coding: utf-8 -*-
"""
Created on Wed Jun 01 17:05:07 2016

@author: anatta
"""


# Required module
import os
import shutil
###part_1 ### looking for files to be copied and obtaining source path  ###
# Function for getting files from a folder
def fetchFiles(pathToFolder, flag, keyWord):
''' fetchFiles() requires three arguments: pathToFolder, flag and 
 keyWord flag must be 'STARTS_WITH' or 'ENDS_WITH' keyWord is a string to 
  search the file's name  Be careful, the keyWord is case sensitive and must
  be exact.  Example: fetchFiles('/Documents/Photos/','ENDS_WITH','.jpg')
returns: _pathToFiles and _fileNames '''

_pathToFiles = []
_fileNames = []

for dirPath, dirNames, fileNames in os.walk(pathToFolder):
if flag == 'ENDS_WITH':
selectedPath = [os.path.join(dirPath,item) for item in 
fileNames if item.endswith(keyWord)]
_pathToFiles.extend(selectedPath)

selectedFile = [item for item in fileNames if 
item.endswith(keyWord)]
_fileNames.extend(selectedFile)

elif flag == 'STARTS_WITH':
selectedPath = [os.path.join(dirPath,item) for item in 
fileNames if item.startswith(keyWord)]
_pathToFiles.extend(selectedPath)

selectedFile = [item for item in fileNames if 
item.startswith(keyWord)]
_fileNames.extend(selectedFile) 

else:
print fetchFiles.__doc__
break

# Try to remove empty entries if none of the required files are 
in directory
try:
_pathToFiles.remove('')
_imageFiles.remove('')
except ValueError:
pass

# Warn if nothing was found in the given path
#if selectedFile == []: 
#print 'No files with given parameters were found 
in:\n', dirPath, '\n'

#print len(_fileNames), 'files were found is searched 
folder(s)' 

#return _pathToFiles, _fileNames
#print _pathToFiles, _fileNames
print 'path to first tuple file is:', _pathToFiles [0]
str1 = ' '.join(_pathToFiles [0]) #convert tuple element 0 to string
print 'length of str1 is: ', len (str1)
str2 = str1.replace(" ", "") #remove white spaces
print 'str2 is', str2
str3 = str2[13:16] #extract rgeistration
print 'str3 is registration:', str3


str4 = 'FLDAT'
print 'str4 is: ', str4
str5 = str3.__add__(str4)
print 'str 5 is: ',str5
str6 = 'H://'
print 'str6 is: ', str5
str7 = str6.__add__(str5)
print 'str7 is: ', str7  

#print _fileNames
print 'Number of files found: ', len(_fileNames)
fetchFiles('H://','ENDS_WITH','.FLD')

 part_2  copying files from sourcePath to destPath

#sourcePath = r'H://TCVFLDAT'
sourcePath = r'str7'
print 'Source path is: ', sourcePath
destPath = r'c://test_o/'
print 'Destination path is: ', destPath
#ls=os.listdir('.')#list current dir
#print('listing current dir\n')
#print(ls)
for root, dirs, files in os.walk(sourcePath):

#figure out where we're going
dest = destPath + root.replace(sourcePath, '')

#if we're in a directory that doesn't exist in the destination folder
#then create a new folder
if not os.path.isdir(dest):
os.mkdir(dest)
print 'Directory created at: ' + dest
else:
print 'Directory already exists:' + dest

for root, dirs, files in os.walk(sourcePath):
#figure out where we're going
dest = destPath + root.replace(sourcePath, '')
filetype = '.FLD'# name the file ext to be copied
print 'All files of this type will be copied', filetype
#loop through all files in the directory
for f in files:

#compute current (old) & new file locations
oldLoc = root + '\\' + f
newLoc = dest + '\\' + f
#print 'Old location is:', oldLoc
#print 'New location is:', newLoc

if not os.path.isfile(newLoc):
try:
#filetype = '.FLD'# name the file ext to be copied
#print 'All files of this 

Re: [Tutor] path directory backslash ending

2013-04-19 Thread eryksun
On Thu, Apr 18, 2013 at 8:51 PM, Jim Mooney  wrote:
>
> But that's in win 7. Is it okay to always omit them in Linux? Python33
> is itself installed with a trailing backslash, so I figured this was a
> Linux habit.

POSIX/Linux uses a forward slash instead of a backslash (py: os.sep),
and the delimiter in PATH is a colon instead of a semicolon (py:
os.pathsep). There's no convention I know of to use trailing slashes.

You might also consider using the PEP 405 "venv" module:

http://www.python.org/dev/peps/pep-0405

When you "activate" the environment it prepends the "Scripts" (or
"bin") directory to PATH. The option "--symlinks" requires an elevated
security token on Windows, but just to create the virtual environment.
If you'd rather copy over the required DLLs, there's a "--upgrade"
option for when you upgrade to Python 3.4, etc.

> An entirely different question as long as I'm here. I have a local
> wamp server with mysql and phpadmin for php so I can test web pages
> locally. What's the equivalent for Python?

Here's a sampling of links. Hopefully a web developer will provide a
more detailed answer.

mod_wsgi
http://code.google.com/p/modwsgi
http://code.google.com/p/modwsgi/wiki/InstallationOnWindows

The wiki has several integration guides for popular frameworks.

mod_wsgi Windows binaries:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi

Python Web Server Gateway Interface v1.0.1
http://www.python.org/dev/peps/pep-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path directory backslash ending

2013-04-18 Thread Jim Mooney
Well, under the principle of least  harm, it appears that since the
trailing backslash causes no harm if omitted, but sometimes does if
allowed, I removed them all.

But that's in win 7. Is it okay to always omit them in Linux? Python33
is itself installed with a trailing backslash, so I figured this was a
Linux habit.

An entirely different question as long as I'm here. I have a local
wamp server with mysql and phpadmin for php so I can test web pages
locally. What's the equivalent for Python?

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path directory backslash ending

2013-04-18 Thread eryksun
On Thu, Apr 18, 2013 at 12:45 PM, Jim Mooney  wrote:
> Minor question. I was adding the Py Script directory to the Win 7
> Path, and noticed that Python33 ends with a backslash but many
> directories do not. Is there a difference? Should I use backslash or
> not preferentially, or doesn't it matter at all? It does seem odd that
> there's no convention for this.

The trailing backslash shouldn't directly matter in the PATH variable,
not as far how the system splits the string on ';' and searches the
directories. It may be an issue with escaping if "%PATH%" is passed as
a quoted argument. We can test this to be sure. Create test.py:

import sys
print(sys.argv[1:])

Now try an example. First without quotes:

C:\>test.py C:\Program Files;C:\Python33\
['C:\\Program', 'Files;C:\\Python33\\']

Obviously the C runtime (not the shell, as would be the case on a
POSIX system)  needs a little help parsing the argument string. We'll
add some quotes around it:

C:\>test.py "C:\Program Files;C:\Python33\"
['C:\\Program Files;C:\\Python33"']

Ack! The trailing backslash was treated as an escape character, so we
end up with a trailing double quote. It works fine if you remove the
trailing backslash:

C:\>test.py "C:\Program Files;C:\Python33"
['C:\\Program Files;C:\\Python33']

OK, so IMHO don't use a trailing backslash.

It's also a convention to not use a trailing backslash in directories
set as environment variables. For example:

C:\>echo %ProgramFiles%
C:\Program Files

This makes it look more natural as part of another path:

C:\>dir /b "%ProgramFiles%\Microsoft SDKs\Windows\v7.0A"
bin
Include
Lib

Note that the shell replaces %ProgramFiles% literally with C:\Program
Files, so quotes are required.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path directory backslash ending

2013-04-18 Thread Dave Angel

On 04/18/2013 12:45 PM, Jim Mooney wrote:

Minor question. I was adding the Py Script directory to the Win 7
Path, and noticed that Python33 ends with a backslash but many
directories do not. Is there a difference? Should I use backslash or
not preferentially, or doesn't it matter at all? It does seem odd that
there's no convention for this.



There's no Python convention.  There may be a Windows convention, but I 
doubt it.  As far as I could tell when I was stuck in Windows, the only 
time a trailing backslash was significant was when there was nothing in 
front of it but a colon and/or a drive letter.


In Linux, applications are free to make their own use of the trailing 
backslash, and I know that rsync does something different with it 
present than without.  But in the PATH, it doesn't matter.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] path directory backslash ending

2013-04-18 Thread Jim Mooney
Minor question. I was adding the Py Script directory to the Win 7
Path, and noticed that Python33 ends with a backslash but many
directories do not. Is there a difference? Should I use backslash or
not preferentially, or doesn't it matter at all? It does seem odd that
there's no convention for this.

-- 
Jim Mooney

Today is the day that would have been tomorrow if yesterday was today
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-15 Thread Adam Bark
On 15 July 2010 17:21, Jim Byrnes  wrote:

> Adam Bark wrote:
>
>> On 14 July 2010 17:41, Jim Byrnes  wrote:
>>
>>  Adam Bark wrote:
>>>
>>>  On 14 July 2010 02:53, Jim Byrnes   wrote:

  Adam Bark wrote:

>
> 
>
>
>  If I use the terminal to start the program it has no problem using the
>
>   file.  There are multiple files in multiple directories so I was
>>
>>>  looking
> for
> a way to just double click them and have them run.  If it turns out
> that
> I
> must make changes to or for each of the files it will be easier to
> just
> keep
> using the terminal.  I've only been using Ubuntu for a few months
> so
> I
> was
> surprised that the program could not see a file that is in the same
> directory.
>
> Regards,  Jim
>
>
>
>  The problem is ubuntu doesn't run the script from the directory
 it's
 in
 so
 it's looking for wxPython.jpg somewhere else.


  OK, I mistakenly thought that double-clicking on file in Nautilus
 would

  take care of the path info.
>>>
>>> In my reply above I also mentioned that I tried by dropping it on a
>>> Launcher on the top panel and that the command the launcher uses is
>>> usr/bin/python2.6.  Is there a way that the command can be changed so
>>> that
>>> it will look in the same directory the python script is in for any
>>> file
>>> it
>>> needs?
>>>
>>> Thanks,  Jim
>>>
>>>
>>>
>> Not sure if you got my previous email but you could try writing the
>> bash
>> script I posted (with the $1 line to get the path) and setting that as
>> your
>> launcher, I think it should work.
>>
>> Let me know if you didn't get it or it doesn't work.
>>
>> HTH,
>> Adam.
>>
>>
>>  I got it, got sidetracked and then forgot to look at it again.
>>  Thanks
>>
> for
> reminding me.  Your idea works, but with one little downside.  The
> directories I am working with are chapters in a book.  So as I move
> from
> chapter to chapter I will need to change the bash script, but this
> seems
> to
> be less typing than using the terminal.
>
>
> Thanks,  Jim
>
>
>  Ok cool, glad it works. It might be possible to get the path so you
 don't
 have to set it each time, try this:

 #!/bin/bash
 IFS="/"
 path=($1)
 cd $(path[0:#path[*]])
 python $1


 # Warning, I'm not exactly a competent bash programmer so this may not
 work
 :-p

 Let me know if you need a hand to fix it,

 HTH,
 Adam.


  I tried the new bash code but when I dropped a file on the launcher it
>>> just
>>> flashed an gave no output.  So I tried running the bash script
>>> (name=runpython) in a terminal and got this error:
>>>
>>> /home/jfb/runpython: line 4: path[0:#path[*]]: command not found
>>> Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
>>> [GCC 4.4.3] on linux2
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>

>>
>>> I know even less about bash than you do, so I don't where to start to
>>> debug
>>> this.
>>>
>>>
>>> Thanks,  Jim
>>>
>>> Ok then, this time it's tested and not just improvised, here we go:
>>>
>>
>> #!/bin/bash
>>
>> script=$1 # Full path for calling the script later
>> orig_IFS=$IFS # This is to reset IFS so that "script" is correct
>> (otherwise
>> has spaces instead of /)
>> IFS="/"
>> path=( $1 )
>> IFS=$orig_IFS
>> last_ind=${#pa...@]} # Works out the length of path
>> let "last_ind -= 1" # Sets last_ind to index of script name
>> len_path=${pa...@]:0:last_ind} # Gets the path without the script name
>> let "len_path=${#len_path[0]} + 1" # This gives the length of the script
>> string upto just before the last /
>> cd ${scri...@]:0:len_path} # cds to the path
>> python script
>>
>>
>> As pretty much my first non-trivial bash script it's probably horrible but
>> it seems to work.
>>
>> HTH,
>> Adam.
>>
>>
> There must be something different in our setups because it did not work for
> me.  If I run it from a terminal I get:
>
> j...@jfb-ubuntu64:~$ /home/jfb/runpython_test bitmap_button.py
> /home/jfb/runpython_test: line 12: cd: b: No such file or directory
> python: can't open file 'script': [Errno 2] No such file or directory
> j...@jfb-ubuntu64:~$
>
> Thanks  Jim
>
>
Oh cock, I missed a $ sign it should be "python $script". Seems to complain
about the path as well though, not sure about that one, I'll get back to you
later.

Adam.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-15 Thread Jim Byrnes

Adam Bark wrote:

On 14 July 2010 17:41, Jim Byrnes  wrote:


Adam Bark wrote:


On 14 July 2010 02:53, Jim Byrnes   wrote:

  Adam Bark wrote:





  If I use the terminal to start the program it has no problem using the


  file.  There are multiple files in multiple directories so I was

looking
for
a way to just double click them and have them run.  If it turns out
that
I
must make changes to or for each of the files it will be easier to
just
keep
using the terminal.  I've only been using Ubuntu for a few months so
I
was
surprised that the program could not see a file that is in the same
directory.

Regards,  Jim




The problem is ubuntu doesn't run the script from the directory it's
in
so
it's looking for wxPython.jpg somewhere else.


  OK, I mistakenly thought that double-clicking on file in Nautilus
would


take care of the path info.

In my reply above I also mentioned that I tried by dropping it on a
Launcher on the top panel and that the command the launcher uses is
usr/bin/python2.6.  Is there a way that the command can be changed so
that
it will look in the same directory the python script is in for any file
it
needs?

Thanks,  Jim




Not sure if you got my previous email but you could try writing the bash
script I posted (with the $1 line to get the path) and setting that as
your
launcher, I think it should work.

Let me know if you didn't get it or it doesn't work.

HTH,
Adam.


  I got it, got sidetracked and then forgot to look at it again.  Thanks

for
reminding me.  Your idea works, but with one little downside.  The
directories I am working with are chapters in a book.  So as I move from
chapter to chapter I will need to change the bash script, but this seems
to
be less typing than using the terminal.


Thanks,  Jim



Ok cool, glad it works. It might be possible to get the path so you don't
have to set it each time, try this:

#!/bin/bash
IFS="/"
path=($1)
cd $(path[0:#path[*]])
python $1


# Warning, I'm not exactly a competent bash programmer so this may not
work
:-p

Let me know if you need a hand to fix it,

HTH,
Adam.



I tried the new bash code but when I dropped a file on the launcher it just
flashed an gave no output.  So I tried running the bash script
(name=runpython) in a terminal and got this error:

/home/jfb/runpython: line 4: path[0:#path[*]]: command not found
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.




I know even less about bash than you do, so I don't where to start to debug
this.


Thanks,  Jim

Ok then, this time it's tested and not just improvised, here we go:


#!/bin/bash

script=$1 # Full path for calling the script later
orig_IFS=$IFS # This is to reset IFS so that "script" is correct (otherwise
has spaces instead of /)
IFS="/"
path=( $1 )
IFS=$orig_IFS
last_ind=${#pa...@]} # Works out the length of path
let "last_ind -= 1" # Sets last_ind to index of script name
len_path=${pa...@]:0:last_ind} # Gets the path without the script name
let "len_path=${#len_path[0]} + 1" # This gives the length of the script
string upto just before the last /
cd ${scri...@]:0:len_path} # cds to the path
python script


As pretty much my first non-trivial bash script it's probably horrible but
it seems to work.

HTH,
Adam.



There must be something different in our setups because it did not work 
for me.  If I run it from a terminal I get:


j...@jfb-ubuntu64:~$ /home/jfb/runpython_test bitmap_button.py
/home/jfb/runpython_test: line 12: cd: b: No such file or directory
python: can't open file 'script': [Errno 2] No such file or directory
j...@jfb-ubuntu64:~$

Thanks  Jim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-14 Thread Adam Bark
On 14 July 2010 17:41, Jim Byrnes  wrote:

> Adam Bark wrote:
>
>> On 14 July 2010 02:53, Jim Byrnes  wrote:
>>
>>  Adam Bark wrote:
>>>
>>> 
>>>
>>>
>>>  If I use the terminal to start the program it has no problem using the
>>>
  file.  There are multiple files in multiple directories so I was
>>> looking
>>> for
>>> a way to just double click them and have them run.  If it turns out
>>> that
>>> I
>>> must make changes to or for each of the files it will be easier to
>>> just
>>> keep
>>> using the terminal.  I've only been using Ubuntu for a few months so
>>> I
>>> was
>>> surprised that the program could not see a file that is in the same
>>> directory.
>>>
>>> Regards,  Jim
>>>
>>>
>>>
>> The problem is ubuntu doesn't run the script from the directory it's
>> in
>> so
>> it's looking for wxPython.jpg somewhere else.
>>
>>
>>  OK, I mistakenly thought that double-clicking on file in Nautilus
>> would
>>
> take care of the path info.
>
> In my reply above I also mentioned that I tried by dropping it on a
> Launcher on the top panel and that the command the launcher uses is
> usr/bin/python2.6.  Is there a way that the command can be changed so
> that
> it will look in the same directory the python script is in for any file
> it
> needs?
>
> Thanks,  Jim
>
>

 Not sure if you got my previous email but you could try writing the bash
 script I posted (with the $1 line to get the path) and setting that as
 your
 launcher, I think it should work.

 Let me know if you didn't get it or it doesn't work.

 HTH,
 Adam.


  I got it, got sidetracked and then forgot to look at it again.  Thanks
>>> for
>>> reminding me.  Your idea works, but with one little downside.  The
>>> directories I am working with are chapters in a book.  So as I move from
>>> chapter to chapter I will need to change the bash script, but this seems
>>> to
>>> be less typing than using the terminal.
>>>
>>>
>>> Thanks,  Jim
>>>
>>>
>> Ok cool, glad it works. It might be possible to get the path so you don't
>> have to set it each time, try this:
>>
>> #!/bin/bash
>> IFS="/"
>> path=($1)
>> cd $(path[0:#path[*]])
>> python $1
>>
>>
>> # Warning, I'm not exactly a competent bash programmer so this may not
>> work
>> :-p
>>
>> Let me know if you need a hand to fix it,
>>
>> HTH,
>> Adam.
>>
>>
> I tried the new bash code but when I dropped a file on the launcher it just
> flashed an gave no output.  So I tried running the bash script
> (name=runpython) in a terminal and got this error:
>
> /home/jfb/runpython: line 4: path[0:#path[*]]: command not found
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>>
>
> I know even less about bash than you do, so I don't where to start to debug
> this.
>
>
> Thanks,  Jim
>
> Ok then, this time it's tested and not just improvised, here we go:

#!/bin/bash

script=$1 # Full path for calling the script later
orig_IFS=$IFS # This is to reset IFS so that "script" is correct (otherwise
has spaces instead of /)
IFS="/"
path=( $1 )
IFS=$orig_IFS
last_ind=${#pa...@]} # Works out the length of path
let "last_ind -= 1" # Sets last_ind to index of script name
len_path=${pa...@]:0:last_ind} # Gets the path without the script name
let "len_path=${#len_path[0]} + 1" # This gives the length of the script
string upto just before the last /
cd ${scri...@]:0:len_path} # cds to the path
python script


As pretty much my first non-trivial bash script it's probably horrible but
it seems to work.

HTH,
Adam.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-14 Thread Jim Byrnes

Adam Bark wrote:

On 14 July 2010 02:53, Jim Byrnes  wrote:


Adam Bark wrote:




  If I use the terminal to start the program it has no problem using the

file.  There are multiple files in multiple directories so I was
looking
for
a way to just double click them and have them run.  If it turns out
that
I
must make changes to or for each of the files it will be easier to just
keep
using the terminal.  I've only been using Ubuntu for a few months so I
was
surprised that the program could not see a file that is in the same
directory.

Regards,  Jim




The problem is ubuntu doesn't run the script from the directory it's in
so
it's looking for wxPython.jpg somewhere else.


  OK, I mistakenly thought that double-clicking on file in Nautilus would

take care of the path info.

In my reply above I also mentioned that I tried by dropping it on a
Launcher on the top panel and that the command the launcher uses is
usr/bin/python2.6.  Is there a way that the command can be changed so
that
it will look in the same directory the python script is in for any file
it
needs?

Thanks,  Jim




Not sure if you got my previous email but you could try writing the bash
script I posted (with the $1 line to get the path) and setting that as
your
launcher, I think it should work.

Let me know if you didn't get it or it doesn't work.

HTH,
Adam.



I got it, got sidetracked and then forgot to look at it again.  Thanks for
reminding me.  Your idea works, but with one little downside.  The
directories I am working with are chapters in a book.  So as I move from
chapter to chapter I will need to change the bash script, but this seems to
be less typing than using the terminal.


Thanks,  Jim



Ok cool, glad it works. It might be possible to get the path so you don't
have to set it each time, try this:

#!/bin/bash
IFS="/"
path=($1)
cd $(path[0:#path[*]])
python $1


# Warning, I'm not exactly a competent bash programmer so this may not work
:-p

Let me know if you need a hand to fix it,

HTH,
Adam.



I tried the new bash code but when I dropped a file on the launcher it 
just flashed an gave no output.  So I tried running the bash script 
(name=runpython) in a terminal and got this error:


/home/jfb/runpython: line 4: path[0:#path[*]]: command not found
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

I know even less about bash than you do, so I don't where to start to 
debug this.


Thanks,  Jim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-14 Thread Adam Bark
On 14 July 2010 02:53, Jim Byrnes  wrote:

> Adam Bark wrote:
>
> 
>
>
>  If I use the terminal to start the program it has no problem using the
> file.  There are multiple files in multiple directories so I was
> looking
> for
> a way to just double click them and have them run.  If it turns out
> that
> I
> must make changes to or for each of the files it will be easier to just
> keep
> using the terminal.  I've only been using Ubuntu for a few months so I
> was
> surprised that the program could not see a file that is in the same
> directory.
>
> Regards,  Jim
>
>

 The problem is ubuntu doesn't run the script from the directory it's in
 so
 it's looking for wxPython.jpg somewhere else.


  OK, I mistakenly thought that double-clicking on file in Nautilus would
>>> take care of the path info.
>>>
>>> In my reply above I also mentioned that I tried by dropping it on a
>>> Launcher on the top panel and that the command the launcher uses is
>>> usr/bin/python2.6.  Is there a way that the command can be changed so
>>> that
>>> it will look in the same directory the python script is in for any file
>>> it
>>> needs?
>>>
>>> Thanks,  Jim
>>>
>>
>>
>> Not sure if you got my previous email but you could try writing the bash
>> script I posted (with the $1 line to get the path) and setting that as
>> your
>> launcher, I think it should work.
>>
>> Let me know if you didn't get it or it doesn't work.
>>
>> HTH,
>> Adam.
>>
>>
> I got it, got sidetracked and then forgot to look at it again.  Thanks for
> reminding me.  Your idea works, but with one little downside.  The
> directories I am working with are chapters in a book.  So as I move from
> chapter to chapter I will need to change the bash script, but this seems to
> be less typing than using the terminal.
>
>
> Thanks,  Jim
>

Ok cool, glad it works. It might be possible to get the path so you don't
have to set it each time, try this:

 #!/bin/bash

IFS="/"
path=($1)
cd $(path[0:#path[*]])
python $1


# Warning, I'm not exactly a competent bash programmer so this may not work
:-p

Let me know if you need a hand to fix it,

HTH,
Adam.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-13 Thread Jim Byrnes

Adam Bark wrote:




If I use the terminal to start the program it has no problem using the
file.  There are multiple files in multiple directories so I was looking
for
a way to just double click them and have them run.  If it turns out that
I
must make changes to or for each of the files it will be easier to just
keep
using the terminal.  I've only been using Ubuntu for a few months so I
was
surprised that the program could not see a file that is in the same
directory.

Regards,  Jim




The problem is ubuntu doesn't run the script from the directory it's in so
it's looking for wxPython.jpg somewhere else.



OK, I mistakenly thought that double-clicking on file in Nautilus would
take care of the path info.

In my reply above I also mentioned that I tried by dropping it on a
Launcher on the top panel and that the command the launcher uses is
usr/bin/python2.6.  Is there a way that the command can be changed so that
it will look in the same directory the python script is in for any file it
needs?

Thanks,  Jim



Not sure if you got my previous email but you could try writing the bash
script I posted (with the $1 line to get the path) and setting that as your
launcher, I think it should work.

Let me know if you didn't get it or it doesn't work.

HTH,
Adam.



I got it, got sidetracked and then forgot to look at it again.  Thanks 
for reminding me.  Your idea works, but with one little downside.  The 
directories I am working with are chapters in a book.  So as I move from 
chapter to chapter I will need to change the bash script, but this seems 
to be less typing than using the terminal.


Thanks,  Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-13 Thread Adam Bark
On 13 July 2010 23:27, Jim Byrnes  wrote:

> Adam Bark wrote:
>
>> On 13 July 2010 14:43, Jim Byrnes  wrote:
>>
>>  Steven D'Aprano wrote:
>>>
>>> My apologizes to Steven and the list, when I replied originally I messed
>>> up
>>> and sent it to him privately which was not my intention.
>>>
>>>
>>>
>>>  On Mon, 12 Jul 2010 03:42:28 am Jim Byrnes wrote:

> I am running Ubuntu.  I downloaded the source code examples for a
> book I purchased.  Some of the examples load image files located in
> the same directory as the program.  If I go to the current directory
> in the terminal the program can use the image files.  However, if I
> use a launcher or the filemanager it pops up an error dialog saying
> the file does not exist even though it is in the same directory.
>
> The program simply uses the files name.  Is there a way without
> editing the source and inserting the full path to run the program
> from a launcher or the filemanager and allow it to see files in the
> current directory?
>

 What file manager are you using? Nautilus? Konqueror? Something else?

>>>
>>> Nautilus. I have it configured to run files with the extension .py when
>>> they are double clicked.
>>>
>>>
>>>  What do you mean, "use a launcher"? Use a launcher to do what? What sort
 of launcher?

>>>
>>> It runs programs and sits on the panel at the top of my Ubuntu desktop.
>>>  The command it uses is usr/bin/python2.6.  These are wxPython examples I
>>> am
>>> working with.
>>>
>>>
>>>  What pops up an error dialog? The launcher?

>>>
>>> I am assuming Python. The title bar of the dialog says Python2 Error, the
>>> message is   Can't load image from file 'wxPython.jpg': file does not
>>> exist.
>>>
>>>
>>>  Which file does it claim doesn't exist? Python? The Python script? The
 image file? What is the exact error message it gives?

>>>
>>> See above.  The line that triggers the error is:  image =
>>> wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
>>>
>>>
>>>  There's probably a way to tell the launcher which working directory to
 use, but of course that depends on the answers to the above questions.


>>> If I use the terminal to start the program it has no problem using the
>>> file.  There are multiple files in multiple directories so I was looking
>>> for
>>> a way to just double click them and have them run.  If it turns out that
>>> I
>>> must make changes to or for each of the files it will be easier to just
>>> keep
>>> using the terminal.  I've only been using Ubuntu for a few months so I
>>> was
>>> surprised that the program could not see a file that is in the same
>>> directory.
>>>
>>> Regards,  Jim
>>>
>>
>>
>> The problem is ubuntu doesn't run the script from the directory it's in so
>> it's looking for wxPython.jpg somewhere else.
>>
>>
> OK, I mistakenly thought that double-clicking on file in Nautilus would
> take care of the path info.
>
> In my reply above I also mentioned that I tried by dropping it on a
> Launcher on the top panel and that the command the launcher uses is
> usr/bin/python2.6.  Is there a way that the command can be changed so that
> it will look in the same directory the python script is in for any file it
> needs?
>
> Thanks,  Jim


Not sure if you got my previous email but you could try writing the bash
script I posted (with the $1 line to get the path) and setting that as your
launcher, I think it should work.

Let me know if you didn't get it or it doesn't work.

HTH,
Adam.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-13 Thread Jim Byrnes

Adam Bark wrote:

On 13 July 2010 14:43, Jim Byrnes  wrote:


Steven D'Aprano wrote:

My apologizes to Steven and the list, when I replied originally I messed up
and sent it to him privately which was not my intention.




On Mon, 12 Jul 2010 03:42:28 am Jim Byrnes wrote:

I am running Ubuntu.  I downloaded the source code examples for a
book I purchased.  Some of the examples load image files located in
the same directory as the program.  If I go to the current directory
in the terminal the program can use the image files.  However, if I
use a launcher or the filemanager it pops up an error dialog saying
the file does not exist even though it is in the same directory.

The program simply uses the files name.  Is there a way without
editing the source and inserting the full path to run the program
from a launcher or the filemanager and allow it to see files in the
current directory?


What file manager are you using? Nautilus? Konqueror? Something else?


Nautilus. I have it configured to run files with the extension .py when
they are double clicked.



What do you mean, "use a launcher"? Use a launcher to do what? What sort
of launcher?


It runs programs and sits on the panel at the top of my Ubuntu desktop.
  The command it uses is usr/bin/python2.6.  These are wxPython examples I am
working with.



What pops up an error dialog? The launcher?


I am assuming Python. The title bar of the dialog says Python2 Error, the
message is   Can't load image from file 'wxPython.jpg': file does not exist.



Which file does it claim doesn't exist? Python? The Python script? The
image file? What is the exact error message it gives?


See above.  The line that triggers the error is:  image =
wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)



There's probably a way to tell the launcher which working directory to
use, but of course that depends on the answers to the above questions.



If I use the terminal to start the program it has no problem using the
file.  There are multiple files in multiple directories so I was looking for
a way to just double click them and have them run.  If it turns out that I
must make changes to or for each of the files it will be easier to just keep
using the terminal.  I've only been using Ubuntu for a few months so I was
surprised that the program could not see a file that is in the same
directory.

Regards,  Jim



The problem is ubuntu doesn't run the script from the directory it's in so
it's looking for wxPython.jpg somewhere else.



OK, I mistakenly thought that double-clicking on file in Nautilus would 
take care of the path info.


In my reply above I also mentioned that I tried by dropping it on a 
Launcher on the top panel and that the command the launcher uses is 
usr/bin/python2.6.  Is there a way that the command can be changed so 
that it will look in the same directory the python script is in for any 
file it needs?


Thanks,  Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-13 Thread Adam Bark
On 13 July 2010 14:43, Jim Byrnes  wrote:

> Steven D'Aprano wrote:
>
> My apologizes to Steven and the list, when I replied originally I messed up
> and sent it to him privately which was not my intention.
>
>
>
> > On Mon, 12 Jul 2010 03:42:28 am Jim Byrnes wrote:
> >> I am running Ubuntu.  I downloaded the source code examples for a
> >> book I purchased.  Some of the examples load image files located in
> >> the same directory as the program.  If I go to the current directory
> >> in the terminal the program can use the image files.  However, if I
> >> use a launcher or the filemanager it pops up an error dialog saying
> >> the file does not exist even though it is in the same directory.
> >>
> >> The program simply uses the files name.  Is there a way without
> >> editing the source and inserting the full path to run the program
> >> from a launcher or the filemanager and allow it to see files in the
> >> current directory?
> >
> > What file manager are you using? Nautilus? Konqueror? Something else?
>
> Nautilus. I have it configured to run files with the extension .py when
> they are double clicked.
>
>
> > What do you mean, "use a launcher"? Use a launcher to do what? What sort
> > of launcher?
>
> It runs programs and sits on the panel at the top of my Ubuntu desktop.
>  The command it uses is usr/bin/python2.6.  These are wxPython examples I am
> working with.
>
>
> > What pops up an error dialog? The launcher?
>
> I am assuming Python. The title bar of the dialog says Python2 Error, the
> message is   Can't load image from file 'wxPython.jpg': file does not exist.
>
>
> > Which file does it claim doesn't exist? Python? The Python script? The
> > image file? What is the exact error message it gives?
>
> See above.  The line that triggers the error is:  image =
> wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
>
>
> > There's probably a way to tell the launcher which working directory to
> > use, but of course that depends on the answers to the above questions.
> >
>
> If I use the terminal to start the program it has no problem using the
> file.  There are multiple files in multiple directories so I was looking for
> a way to just double click them and have them run.  If it turns out that I
> must make changes to or for each of the files it will be easier to just keep
> using the terminal.  I've only been using Ubuntu for a few months so I was
> surprised that the program could not see a file that is in the same
> directory.
>
> Regards,  Jim


The problem is ubuntu doesn't run the script from the directory it's in so
it's looking for wxPython.jpg somewhere else.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-13 Thread Jim Byrnes

Steven D'Aprano wrote:

My apologizes to Steven and the list, when I replied originally I messed 
up and sent it to him privately which was not my intention.



> On Mon, 12 Jul 2010 03:42:28 am Jim Byrnes wrote:
>> I am running Ubuntu.  I downloaded the source code examples for a
>> book I purchased.  Some of the examples load image files located in
>> the same directory as the program.  If I go to the current directory
>> in the terminal the program can use the image files.  However, if I
>> use a launcher or the filemanager it pops up an error dialog saying
>> the file does not exist even though it is in the same directory.
>>
>> The program simply uses the files name.  Is there a way without
>> editing the source and inserting the full path to run the program
>> from a launcher or the filemanager and allow it to see files in the
>> current directory?
>
> What file manager are you using? Nautilus? Konqueror? Something else?

Nautilus. I have it configured to run files with the extension .py when 
they are double clicked.


> What do you mean, "use a launcher"? Use a launcher to do what? What sort
> of launcher?

It runs programs and sits on the panel at the top of my Ubuntu desktop. 
 The command it uses is usr/bin/python2.6.  These are wxPython examples 
I am working with.


> What pops up an error dialog? The launcher?

I am assuming Python. The title bar of the dialog says Python2 Error, 
the message is   Can't load image from file 'wxPython.jpg': file does 
not exist.


> Which file does it claim doesn't exist? Python? The Python script? The
> image file? What is the exact error message it gives?

See above.  The line that triggers the error is:  image = 
wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)


> There's probably a way to tell the launcher which working directory to
> use, but of course that depends on the answers to the above questions.
>

If I use the terminal to start the program it has no problem using the 
file.  There are multiple files in multiple directories so I was looking 
for a way to just double click them and have them run.  If it turns out 
that I must make changes to or for each of the files it will be easier 
to just keep using the terminal.  I've only been using Ubuntu for a few 
months so I was surprised that the program could not see a file that is 
in the same directory.


Regards,  Jim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-11 Thread Steven D'Aprano
On Mon, 12 Jul 2010 03:42:28 am Jim Byrnes wrote:
> I am running Ubuntu.  I downloaded the source code examples for a
> book I purchased.  Some of the examples load image files located in
> the same directory as the program.  If I go to the current directory
> in the terminal the program can use the image files.  However, if I
> use a launcher or the filemanager it pops up an error dialog saying
> the file does not exist even though it is in the same directory.
>
> The program simply uses the files name.  Is there a way without
> editing the source and inserting the full path to run the program
> from a launcher or the filemanager and allow it to see files in the
> current directory?

What file manager are you using? Nautilus? Konqueror? Something else?

What do you mean, "use a launcher"? Use a launcher to do what? What sort 
of launcher?

What pops up an error dialog? The launcher?

Which file does it claim doesn't exist? Python? The Python script? The 
image file? What is the exact error message it gives?

There's probably a way to tell the launcher which working directory to 
use, but of course that depends on the answers to the above questions.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-11 Thread Adam Bark

On 11/07/10 18:42, Jim Byrnes wrote:
I am running Ubuntu.  I downloaded the source code examples for a book 
I purchased.  Some of the examples load image files located in the 
same directory as the program.  If I go to the current directory in 
the terminal the program can use the image files.  However, if I use a 
launcher or the filemanager it pops up an error dialog saying the file 
does not exist even though it is in the same directory.


The program simply uses the files name.  Is there a way without 
editing the source and inserting the full path to run the program from 
a launcher or the filemanager and allow it to see files in the current 
directory?


Thanks,  Jim

Maybe create a bash script to call the python code something like:

#!/bin/bash

cd /directory/the/scripts/are/in
python script_name

HTH,
Adam.

PS if you want to use the same script for any python script you could 
change the last line to:


python $1

and call the bash script with the python script as the first argument
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Path?

2010-07-11 Thread Jim Byrnes
I am running Ubuntu.  I downloaded the source code examples for a book I 
purchased.  Some of the examples load image files located in the same 
directory as the program.  If I go to the current directory in the 
terminal the program can use the image files.  However, if I use a 
launcher or the filemanager it pops up an error dialog saying the file 
does not exist even though it is in the same directory.


The program simply uses the files name.  Is there a way without editing 
the source and inserting the full path to run the program from a 
launcher or the filemanager and allow it to see files in the current 
directory?


Thanks,  Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path to executing .py file

2009-04-14 Thread tiefeng wu
>
> is there some way to get path to my executing script, so I can replaced
>> "os.getcwd()" in above line?
>>
>
> Look at the recent thread on creating exe files with py2exe.
> Investigate the __file__  variable...


thanks, Alan Gauld. thanks for your patience for such a trivial question:)


> shutil.rmtree(svn_repos_copy_dir)
>>
>> I got error "Access denied!" Is that mean my script has no power to delete
>> it?
>>
>
> More likely the user running the script does not have the correct
> access permissions or someone else is using the repository at
> the time thus locking it.


as I mentioned, that's a COPY of svn repository, I'm sure there's no one
locking it
I tried manually delete it by 'del' and 'rm' under command line, and I got
same error,
but deleting in windows explorer (with same user login as script was called)
with no
problem.

if this question has nothing concerned with this mailing list, I'm sorry an
please ignore it.

tiefeng wu
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path to executing .py file

2009-04-14 Thread Alan Gauld


"tiefeng wu"  wrote


is there some way to get path to my executing script, so I can replaced
"os.getcwd()" in above line?


Look at the recent thread on creating exe files with py2exe.
Investigate the __file__  variable...


shutil.rmtree(svn_repos_copy_dir)

I got error "Access denied!" Is that mean my script has no power to 
delete

it?


More likely the user running the script does not have the correct
access permissions or someone else is using the repository at
the time thus locking it.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] path to executing .py file

2009-04-13 Thread tiefeng wu
Hello everybody!
I'm working on my code repository (svn) auto-backup script which get hotcopy
of svn repository
directory to a directory named by date in same location where script file
is, it executed by a timer
program every 00:00 clock. Everything works fine when I'm testing by double
click it. But when
auto execute it, I got unexpected behavior. The backup directory will be
created under
"C:\Documents and settings\username". I create backup directory by this way:

os.mkdir(os.getcwd() + arch_dir)

is there some way to get path to my executing script, so I can replaced
"os.getcwd()" in above
line?

And I have another question, I've tried remove a copy of svn repository by
calling

shutil.rmtree(svn_repos_copy_dir)

I got error "Access denied!" Is that mean my script has no power to delete
it? Is there some way to
give the right to do that?

thanks

tiefeng wu
2009-04-14
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path to use for setting the environment variable PYTHONDOCS?

2007-03-07 Thread Dick Moores
At 03:15 AM 3/7/2007, Kent Johnson wrote:
>Dick Moores wrote:
> > At 02:41 PM 3/6/2007, Alan Gauld wrote:
> >
> >> "Dick Moores" <[EMAIL PROTECTED]> wrote
> >>
> >>> Sorry, topic and keyword documentation is not available because the
> >>> Python
> >>> HTML documentation files could not be found.  If you have installed
> >>> them,
> >>> please set the environment variable PYTHONDOCS to indicate their
> >>> location.
> >>> 
> >>>
> >>> I have Python 2.5. Where are the HTML documentation files?
> >> No idea, but I suspect the answer will be platform specific.
> >> Which platform are you using?
> >
> > Sorry, Win XP.
>
>I don't think the Windows installer for Python includes the HTML docs.
>You can download them from here:
>http://docs.python.org/download.html

Thanks, Kent. Got 'em and installed 'em.

Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path to use for setting the environment variable PYTHONDOCS?

2007-03-07 Thread Kent Johnson
Dick Moores wrote:
> At 02:41 PM 3/6/2007, Alan Gauld wrote:
> 
>> "Dick Moores" <[EMAIL PROTECTED]> wrote
>>
>>> Sorry, topic and keyword documentation is not available because the
>>> Python
>>> HTML documentation files could not be found.  If you have installed
>>> them,
>>> please set the environment variable PYTHONDOCS to indicate their
>>> location.
>>> 
>>>
>>> I have Python 2.5. Where are the HTML documentation files?
>> No idea, but I suspect the answer will be platform specific.
>> Which platform are you using?
> 
> Sorry, Win XP.

I don't think the Windows installer for Python includes the HTML docs. 
You can download them from here:
http://docs.python.org/download.html

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path to use for setting the environment variable PYTHONDOCS?

2007-03-06 Thread Dick Moores
At 02:41 PM 3/6/2007, Alan Gauld wrote:

>"Dick Moores" <[EMAIL PROTECTED]> wrote
>
> > Sorry, topic and keyword documentation is not available because the
> > Python
> > HTML documentation files could not be found.  If you have installed
> > them,
> > please set the environment variable PYTHONDOCS to indicate their
> > location.
> > 
> >
> > I have Python 2.5. Where are the HTML documentation files?
>
>No idea, but I suspect the answer will be platform specific.
>Which platform are you using?

Sorry, Win XP.

Dick

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] path to use for setting the environment variable PYTHONDOCS?

2007-03-06 Thread Alan Gauld

"Dick Moores" <[EMAIL PROTECTED]> wrote

> Sorry, topic and keyword documentation is not available because the 
> Python
> HTML documentation files could not be found.  If you have installed 
> them,
> please set the environment variable PYTHONDOCS to indicate their 
> location.
> 
>
> I have Python 2.5. Where are the HTML documentation files?

No idea, but I suspect the answer will be platform specific.
Which platform are you using?

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] path to use for setting the environment variable PYTHONDOCS?

2007-03-06 Thread Dick Moores
===
 >>>help('assert')

Sorry, topic and keyword documentation is not available because the Python
HTML documentation files could not be found.  If you have installed them,
please set the environment variable PYTHONDOCS to indicate their location.


I have Python 2.5. Where are the HTML documentation files?

Thanks,

Dick Moores

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor