Python Forum
English interpretation of the following file handing snippet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
English interpretation of the following file handing snippet
#1
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
Reply
#2
Now you have a theory.

So test it yourself with a sample script and sample data.
Reply
#3
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.
Reply
#4
Install Python3.7.x (not Python2.x)
Reply
#5
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)"
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 350 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,590 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  [Solved] Trying to rerun a snippet of code paracel 3 1,147 Jul-17-2022, 01:49 PM
Last Post: paracel
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,230 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  write a program which prints the day in english ben1122 10 3,967 Jul-25-2021, 05:55 PM
Last Post: ben1122
  vars() can't be used in list interpretation? Cheng 3 2,099 Jun-22-2021, 11:59 AM
Last Post: snippsat
  This is an Object - Code interpretation JoeDainton123 2 1,795 Jun-16-2021, 08:11 PM
Last Post: snippsat
  More non english characters johnboy1974 8 4,439 Apr-23-2021, 02:35 PM
Last Post: snippsat
  TreeTagger : parameter file invalid : english.par Raph0909 0 2,795 Apr-12-2019, 12:12 PM
Last Post: Raph0909
  python code snippet chinna 4 2,657 Mar-15-2019, 06:26 AM
Last Post: chinna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020