Python Forum
English interpretation of the following file handing snippet - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: English interpretation of the following file handing snippet (/thread-18749.html)



English interpretation of the following file handing snippet - mortch - May-30-2019

update
After reading other posts in this subforum, I think my question might be better suited to general help, but I can't find the delete or move option. I can leave it here but if some mod wants to move it, that's good too.

Summary
What is the English interpretation of the following snippet



for f in oldFiles:
   archFile = '/'.join( f[f.find( ':' )+1:].split( '\\' ) )
   zf.write( f, archFile )
So far I have this being...
For all the files in oldfiles, find the position of the first colon. Move to the next position, then split it up into parts around every backslash until the end of the string. Then join them together with a forward slash between each part.

Does this just replace backslashes for forward slashes?
What does finding the colon do? Does it only replace slashes after this point?


Background
I am reading through my first ever python script. At work and the file archiving process stopped working and the python people away for a few weeks. I am working on creating a test harness, and running it, but waiting on IT to be allowed to install python.

Thanks for any help. Typing it out probably got me half way to understanding this step


RE: English interpretation of the following file handing snippet - heiner55 - May-30-2019

Now you have a theory.

So test it yourself with a sample script and sample data.


RE: English interpretation of the following file handing snippet - mortch - May-30-2019

Yep, that's the obvious, but I don't have python installed yet.

As often happens when writing it out, I realise what the obvious is.
Will run in on my own PC at home tonight and hope IT can get it installed tomorrow.

Steep learning curve. ta

Thanks Buran

have read the page and will use tags in future.


RE: English interpretation of the following file handing snippet - heiner55 - May-30-2019

Install Python3.7.x (not Python2.x)


RE: English interpretation of the following file handing snippet - perfringo - May-30-2019

It good to start with Python interpretation:

>>> f = r"C:\path\file"
>>> '/'.join( f[f.find( ':' )+1:].split( '\\' ))
'/path/file'
>>> s = r"\\server\share\path\file"
>>> '/'.join( s[s.find( ':' )+1:].split( '\\' ) ) 
'//server/share/path/file'
Disassembling step by step:

str.find method: Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation. Return -1 on failure.

>>> f.find(':')
1
>>> s.find(':')
-1
So the code f.find(':') + 1 in spoken language can be represented as: "if there is ':' give me next element index, if not give zero". This is value to be used in slice notation, so it can be modified/extended: "give me slice starting right after ':' if there is one, otherwise start from beginning"

Now add split and join:

"join with '/' list of elements which is obtained by splitting on '\\' the slice of string right after ':' if present otherwise from beginning (whole string)"


RE: English interpretation of the following file handing snippet - mortch - May-30-2019

perfringo!!!

Your two examples worked for me. Got my head around it now.

It seems to convert window file paths into unix ones, all in one line. quite lovely really.

Appreciate your brainspace and choice to help.