Im new to python and got stuck while processing a .bat file  1) I have a .bat file that contains the following lines:  set local_dir=%~d0%~p0  set LVMain_StartAddress=0xfaff set LVMain_EndAddress=0x0f4ff set HVMain_StartAddress=0x01d00 set HVMain_EndAddress=0x095ff set LVMain_StartAddressMerged=0x0D000 set LVMain_EndAddressMerged=0x0fcff set HVMain_StartAddressMerged=0x01001d00 set HVMain_EndAddressMerged=0x010095dd   2) A python script runs through the file to look for the line "set LVMain_StartAddress=0xfaff"  3) When it finds the line, I want to replace the hex address with a new one. Sometimes the new hex address is 5 characters long  4) Below is the python code I wrote to do this:  # write LVMain start address in .bat file with open("..\..\..\Tools\Scripts\GenerateRomTestChecksums.bat", "r+") as f:      #read lines individually until we find where to write LvMain Start address     BatFileLine = f.readline()     while( (LVMain_StartPattern in BatFileLine ) == False ):         BatFileLine = f.readline()      #set marker back to the beginning of the line     LineMarker = int(f.tell()) - (int(len(BatFileLine)) + 0x01)     f.seek(LineMarker)      #split string where address starts to write new address in file     name, hex_address = BatFileLine.split('0x')     #append new address and carriage return     name = name + APPL_End_address + "\n"     print(len(name))       contents = f.read(len(BatFileLine)).replace(BatFileLine,name)     f.seek(LineMarker)     f.write(name)   This is part of a larger function and "APPL_End_address" is given as a  parameter  5) When I run this code, and the new hex address is 5 hex digits wide, I get the following output:   It seems the new hex address is written correctly, but the first character of the next line erased. Any ideas why?
6617
I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.  What is the best way to do this, within the following code?  f = open(file) for line in f:     if line.contains('foo'):         newline = line.replace('foo', 'bar')         # how to write this newline back to the file
33483
Preface: Every couple of days there comes a question of this type, which is easy to solve with sed, but takes time to explain. I'm writing this question and answer, so I can later refer to this generic solution and only explain the adaption for the specific case. Feel free to contribute.  I have files with variable definitions. Variables consist of uppercase letters or underscore _ and their values follow after the :=. The values can contain other variables. This is Gnom.def:  NAME:=Gnom FULL_NAME:=$FIRST_NAME $NAME FIRST_NAME:=Sman STREET:=Mainstreet 42 TOWN:=Nowhere BIRTHDAY:=May 1st, 1999   Then there is another file form.txt with a template form:  $NAME Full name: $FULL_NAME Address: $STREET in $TOWN Birthday: $BIRTHDAY Don't be confused by $NAMES   Now I want a script which replaces the variables (marked with $ and the identifier) in the form by the definitions in the other file, recursively, if necessary, so I get this text back:  Gnom Full name: Sman Gnom Address: Mainstreet 42 in Nowhere Birthday: May 1st, 1999 Don't be confused by $NAMES   The last line is to ensure that no substrings of variables get replaced accidentally.
51237
I would like to drag and drop my data file onto a Python script and have it process the file and generate output. The Python script accepts the name of the data file as a command-line parameter, but Windows Explorer doesn't allow the script to be a drop target.  Is there some kind of configuration that needs to be done somewhere for this work?
62614
I have one file that contains the following:  10.48.29.68 doggy 10.65.8.184 kitty 10.48.15.104 froggy   I need to insert the contents of this file into my hosts file after the line bc group in the hosts file.  The hostfile of the server already has hundreds of entries. Bc is a group here in our hosts file. I want code that reads the ip addresses and hostnames in my file above and put those entries in the hosts file below the heading bc group.  The new file above should be written in the hosts file after keyword bc group.  For example, if my hosts file contains these lines:  10.59.12.232 bc4 10.48.29.68 xy9 bc group 10.63.71.136 bc2 10.63.71.214 bc3   I need to convert the hosts file to this:  10.59.12.232 bc4 10.48.29.68 xy9 bc group 10.48.29.68 doggy 10.65.8.184 kitty 10.48.15.104 froggy 10.63.71.136 bc2 10.63.71.214 bc3   I need to append all of the lines from my first file after the line bc group in my hosts file, and then continue with the remaining lines in the hosts file.
3312
I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process.   For example, let's say I have three files. Using :   script_1.py calls script_2.py.  In turn, script_2.py calls script_3.py.    How can I get the file name and path of script_3.py, from code within script_3.py, without having to pass that information as arguments from script_2.py?  (Executing os.getcwd() returns the original starting script's filepath not the current file's.)
78606
I'm just starting to get into Python scripting for work.  I'm currently creating a script to automate a process.  Basically, it asks the user the client name, gets a projection if available, creates directory on C: drive for client, creates file geodatabase specific to client, creates required dataset, and creates feature classes specific to clients data. Eventually, it will also add the required fields to each feature class and probably some other things.  I started this not really knowing the proper etiquette of Python scripting for ArcMap. But what I've created so far will only run outside ArcMap I believe.   Is this acceptable?   Instead of getting user input through arcpy.getparamaterastext(), which I just found out about, I'm using raw_input().   Is this okay?   It does work, I'm just not sure if this is a proper way to do scripting.  Here's the code I have so far.  import sys import arcpy import os  #Records name of the client client = raw_input("Enter the name of the client: (letters and underscores only) \n")  #Records filepath of client to be created clientpath = "C:/" + client  #Inquires if projection file exists projection = raw_input("Is there a .prj or .shp available with correct projection? Y or N \n")  #Records the projection location if available if projection.upper() == "Y":     spatialr = raw_input("Drag the .prj or .shp here to record the filepath \n")     nspatialr = spatialr.replace('"', "") elif projection.upper() == "N":     alert = raw_input("You must add the spatial reference manually, hit enter to continue. \n") elif projection.upper() != "N" or "Y":     exit = raw_input("That is not a valid response. Try again. \n")     sys.exit()  #Checks if client folder exists; if not, creates one if not os.path.exists(clientpath):     os.makedirs(clientpath)  #Variable for file geodatabase location FGBpath = clientpath + "/" + client + ".gdb"  #Checks if client file geodatabase exists; if not, creates one if not arcpy.Exists(FGBpath):     arcpy.CreateFileGDB_management(clientpath, client)  #Variable for dataset location FDatasetpath = clientpath + "/" + client + ".gdb" + "/Network"  #Checks if dataset exists; if not, creates one if not arcpy.Exists(FDatasetpath):     if projection.upper() == "Y":         arcpy.CreateFeatureDataset_management(FGBpath, "Network", nspatialr)     elif projection.upper() == "N":         arcpy.CreateFeatureDataset_management(FGBpath, "Network")  #Variable for cable feature class location FCcablepath = clientpath + "/" + client + ".gdb" + "/Network" + "/cable"  #Checks if cable feature class exists; if not, creates one if not arcpy.Exists(FCcablepath):     if projection.upper() == "Y":         arcpy.CreateFeatureclass_management (FDatasetpath, "cable", "POLYLINE", "", "", "", nspatialr)     elif projection.upper() == "N":         arcpy.CreateFeatureclass_management (FDatasetpath, "cable", "POLYLINE")  #Variable for splice point feature class location FCsplicepath = clientpath + "/" + client + ".gdb" + "/Network" + "/splice_point"  #Checks if splice point feature class exists; if not, creates one if not arcpy.Exists(FCsplicepath):     if projection == 'Y' or projection == 'y':         arcpy.CreateFeatureclass_management (FDatasetpath, "splice_point", "POINT", "", "", "", nspatialr)     elif projection == 'N' or projection == 'n':         arcpy.CreateFeatureclass_management (FDatasetpath, "splice_point", "POINT")  exit = raw_input("\n\n File geodatabase, dataset, and the cable \n and splice point feature classes successfully created. \n\n Hit enter to exit.")   I still have some work to do, like adding the needed fields.
21349
How do you iterate over each file in a directory with a .bat or .cmd file?  For simplicity please provide an answer that just echoes the filename or file path.
89847
I have a setup.bat file which install some applications and i want to execute it.  This is content of .bat file :  @echo off cls Rip_7z.exe x Rip0.7z cls del Rip0.7z cls Rip_7z.exe x Rip1.7z cls del Rip1.7z   I think the Rip_7z.exe file extracts the Rip0.7z and Rip1.7z files and builds the app.   How can I do this in the Ubuntu 11.04 Terminal?
2573
Are there any alternatives to the code below:  startFromLine = 141978 # or whatever line I need to jump to  urlsfile = open(filename, "rb", 0)  linesCounter = 1  for line in urlsfile:     if linesCounter &gt; startFromLine:         DoSomethingWithThisLine(line)      linesCounter += 1   If I'm processing a huge text file (~15MB) with lines of unknown but different length, and need to jump to a particular line which number I know in advance? I feel bad by processing them one by one when I know I could ignore at least first half of the file. Looking for more elegant solution if there is any.
43815
So I found this python script on another post here and was trying to adjust it for my needs.  I'm a very novice python user so I'm struggling with how to modify the script.  I have a Feature Class stored in a feature Dataset that I want to search a field for duplicate values and populate a new field with Y for duplicate or N for none.  The below script is what I found that looks like it will work once I find a way to drill down into my file geodatabase.          from arcpy import *          inShapefile = pointsShapefile          checkField = "xyCombine"         updateField = "dplicate"         #List of values found once         occursOnce = []        #list of values found twice         occursTwice = []       cursor = da.SearchCursor (inShapefile, [checkField])           for row in cursor:           #Check value is not null        if row[0]:     #If not already found to occur twice, proceed     if not row[0] in occursTwice:         #If hasn't occured once yet         if not row[0] in occursOnce:             #Add to occurs once list             occursOnce.append (row[0])         #If value has already been found once         else:             #Add to occurs twice list (duplicates)             occursTwice.append (row[0])     del cursor       cursor = da.UpdateCursor (inShapefile, [checkField, updateField])      for row in cursor:       #Check value is not null         if row[0]:            #check if value in occursTwice list (i.e. is duplicate)     if row[0] in occursTwice:         row[1] = "Y"     else:         row[1] = "N"     cursor.updateRow(row)    del cursor
103376
I am looping through a text file, using 'while read'. I read three variables from each line - two filenames and a decimal number. I know that this works in a vanilla setup (loop, read line, echo line, extract variables, nothing else), because I have tested it on my input file.  I am getting an odd result, though, once I add in the meat of my routine - which is a call to ffmpeg to merge the audio and video streams represented by the filenames. Successive 'read' operations omit leading characters from the line. I read  that calls for 'read' input within a 'while read' loop can cause problems, and wonder if the ffmpeg call is doing something that confuses bash, but this seems unlikely and bizarre! NB: I don't really understand the ffmpeg call - I got it from an answer to another question.  Any help would be appreciated!  The first 'read' works as expected (and the ffmpeg command completes):  LINE  1 vidsNeedingSound/448£generic@06_10_16-09_30_47.mp4 soundToAdd/448£generic@06_10_16-12_11_54.wav 6.988354 V: vidsNeedingSound/448£generic@06_10_16-09_30_47.mp4   A: soundToAdd/448£generic@06_10_16-12_11_54.wav   D: 6.988354   The second 'read' loses 36 characters from the beginning of the line (and the ffmpeg command fails because the file represented by the variable V which takes the first item of the line doesn't point at a file):  LINE  2 6-09_30_47.mp4 soundToAdd/452£generic@06_10_16-12_11_54.wav 9.64663 V: 6-09_30_47.mp4   A: soundToAdd/452...   The third 'read' works as expected (and the ffmpeg command completes):  LINE  3 vidsNeedingSound/452£left@06_10_16-09_30_47.mp4 soundToAdd/452£left@06_10_16-12_11_54.wav 9.862118 V: vidsNeedingSound/452£left@06_10_16-09_30_47.mp4   A: soundToAdd/452...   The fourth 'read' loses 37 character (one more than the last fail) from the beginning of the line (and the ffmpeg command fails):  LINE  4 09_30_47.mp4 soundToAdd/452£right@06_10_16-12_11_54.wav 9.431392 V: 09_30_47.mp4   A: soundToAdd/452....   That's the pattern. When not preceded by a successful call to ffmpeg, the 'read' works as expected. After each successive successful ffmpeg call, characters are omitted from the beginning of the line: 36 characters to begin with, then 37, 38, 39...  The clincher is that when ffmpeg fails after a 'successful' read (due to another problem), the following 'read' works as expected.  CONCLUSION: a successful ffmpeg call results in a read fail on the next iteration through the loop.  Why does this happen, and how can I stop it?  Here's my code: ...  # Loop through lines in the merge file IT_COUNT=1 while read MERGE_LINE                do              echo "LINE " $IT_COUNT     ((IT_COUNT+=1))     echo $MERGE_LINE   # testing use     read VID_FILE AUD_FILE DURATION &lt;&lt;&lt; "$MERGE_LINE" # Get the data from each line     echo "V: $VID_FILE   A: $AUD_FILE   D: $DURATION"         # testing use ....     # add audio to video     ffmpeg -i $VID_FILE -i $AUD_FILE -filter_complex "aevalsrc=0:d=$AUD_SHIFT[s1];[s1][1:a]concat=n=2:v=0:a=1[aout]" -c:v copy -map 0:v -map [aout] $FILE_OUT -hide_banner  done &lt;$MERGE_FILE     Thanks for looking!
83894
I have a file containing a a large number of lines, each of which contains a bunch of numbers which are separated by spaces. I process this data in a pipe in some way, and then I want to collapse the multiple lines into a single line of all the numbers separated by spaces.  Is there a standard command-line utility I can use to do this? It seems like most line-by-line utilities won't mess with the newlines...
48268
I have some simple python code that searches files for a string e.g. path=c:\path, where the c:\path part may vary. The current code is:  def find_path(i_file):     lines = open(i_file).readlines()     for line in lines:         if line.startswith("Path="):             return # what to do here in order to get line content after "Path=" ?   What is a simple way to get the text after Path=?
16457
I'm writing a lecture with many source codes for example. The codes are stored in severus different files. I use listing and minted packages for formatting the sources.  I would like add character at the begin of every line of some codes like:   Bash shell ($ or #) Python shell (>>>) Matlab (>>) Other shells (>)   I tried some solutions which have given on:       But both solutions have the same problem when I try:  \begin{foo_environment}      \input{bar.txt}  \end{foo_environment}   In both solutions the linebreaking are ingnored  I have tried executing a bash command:  sed 's/^/$ /' bar.sh &gt; bar.txt   This works locally but I can't do the same in  and   This is a sample code bar.sh:  cd .. mount /dev/sda3 /mnt/ cd ~/.ssh/ ll cd ~ ls -al   The result must be:  $ cd .. $ mount /dev/sda3 /mnt/ $ cd ~/.ssh/ $ ll $ cd ~ $ ls -al
54930
I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --option "LINE 2", etc. The output of the command is written to another file. How do I go about doing that? I don't know where to start.
104865
How can I access (read/write) m values of a line Shapefile using pyqgis?   I know how to do it with arcpy cursors ( ) but I don't know how to do it with pyqgis.   Is it possible with the QGIS python library?
90369
Here is the code:  import bpy import csv import sys csv_path = sys.argv[1]  file = open(csv_path) reader = csv.reader(file)  header = next(reader) # 1st line is header  data = [ ] for row in reader:     name0 = row[0]     name1 = row[0]     pathout = 'test-videos/' + row[5] + '.m4v'     s = bpy.context.scene     f = s.objects['FirstName']     f.data.body = name0     f1 = s.objects['FirstName01']     f1.data.body = name1+'?'     bpy.context.scene.render.filepath = pathout     bpy.ops.render.render(animation=True)  bpy.ops.wm.quit_blender()   And here is the error message:  Read blend: /PI-SAT/proj001/proj001-master.blend Traceback (most recent call last): File "/PI-SAT/proj001/proj001master.py", line 9, in &lt;module&gt; header = next(reader) # 1st line is header File"/Applications/Blender/blender.app/Contents/Resources/2.79/python/ lib/python3.5/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xef in position 23: invalid continuation byte Error: File format is not supported in file '/PI-SAT/proj001/test.csv' ... opened default scene instead; saving will write to /PISAT/proj001/test.csv   I have read the file with a different script using the exact same code as in lines 2 - 15 and then printing the names and pathout. Running the script from the command line, not from within Blender, it works perfectly.  I have also run the part of the script within the for loop outside of the loop as a script from the command line and it works perfectly.  What am I missing about the CSV file? I created it in Excel on my Mac; is that a problem?