I want to replace a line in my file with new line(actually I have to insert some content in the line). The line actually contains AC_CONFIG_FILES([]) I have to replace this line with a new line by adding some makefile arguments based on some list. Then I constructed a new string and did replacement in the file. Is there any efficient way of doing this?  # 'subdirs' is the list which contains makefile arguments rstring = "AC_CONFIG_FILES([Makefile" for t in subdirs:     rstring = rstring+' src/'+t+'/Makefile' rstring += '])' print rstring # 'fname' is the file in which replacement have to be done #  i is used for indexing in 'insert' function # 'rline' is the modified line fname = 'configure.ac' i = 0 with open(fname,'r') as f:       modlines=[]       for line in f.readlines():           if 'AC_CONFIG_FILES' in line:                   modlines.insert(i,rstring+'\n')                   i = i+1                   continue           modlines.insert(i,line)           i = i+1 with open(fname,'w') as out:       for i in range(len(modlines)):           out.write(modlines[i])
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
How can I skip every even item in the enumerate environment without having to continually type \setcounter{enumi}{5}? I am still learning to program macros, so I spologize if this is a ridiculously easy question.   Just to be clear, I would like something like   1. *Something.* 3. *Somthing else.* 5. *A third something.*  ...As opposed to   1. Something.  2. A second something.  3. A tertiary Something. So I had been literally trying to fix my windows not being able to boot at all for more than 24 hours straight till now. I almost read every single article and troubleshooting step on the web to find a solution but nothing seems to work. I will keep it short as much as I can.  I did all of the following and it was:  "Reset this PC" gave an error "The required drive partition is missing"  "Refresh this PC" gave an error "The drive where windows is installed is locked. Please unlock and try again"  "System restore" gave an error something I don't actually remember but its like "Choose your OS and then try again"  "Automatic Repair" just failed to fix it and reverted me back to advanced options.   After all of that, I did  bootrec /fixboot bootrec /fixmbr bootrec /scanos bootrec /rebuildbcd bcdboot d:\windows (D is where I have the windows installed and C is for system reserved) bcdboot d:\windows /s c: /f ALL   All of these command prompt lines failed giving multiple errors like:  For REBUILDBCD, it said "Identified total windows installations: 1" then clicked "Y" to add it to the list and then it said "The requested system device cannot be found"  For bcdboot d:\windows it said Failure when attempting to copy boot files"   I wiped the disk multiple times using diskpart and installed fresh copies of windows on it 3 times but each time the installation will finish stage 1 (at a very slow pace) then when it restarts to continue installation, it simply won't boot (Windows logo and a spinning dot circle below forever)  I gave up and tried a totally new HDD and installed windows to it, it booted so normal and fast actually so I made sure it was not an issue with the windows.  Now I am booted from the new HDD and reformatted the old HDD and working on data recovery using easeus and it is finding my stuff luckily. It is also noticeable that the old HDD is making some noise while it is reading now as I scan for lost files.  My ultimate question is, will this HDD ever be able to used again? Even as a storage drive not as a drive I boot from? Because it is being seen through the bios boot menu and I see it in the disk management with healthy status. Can this HDD be fixed or I'm better off stop using it?  Here is a screenshot of the health status through SMART using HD Tune Pro: Recovering broken or deleted NTFS partitions About two weeks ago I had a problem with my PC that my brother wrote a question about at .  I still haven't solved the problem, so my PC is unusable now.  Now I think that it would be simpler to re-install both Win7 &amp; Ubuntu than repair them, but there're some data on their partitions. I want to recover this data.  These partitions are not mountable for some reason. I don't know why.  sudo fdisk -l about the problematic partitions:     Device    Boot      Start         End      Blocks   Id  System /dev/sda1            2048     147912703    73955328   83 Linux /dev/sdb2            206848   174079999    86936576    7  HPFS/NTFS/exFAT   And in GParted:      And the error of the mount command:  Failed to mount '/dev/sdb2': Invalid argument The device '/dev/sdb2' doesn't seem to have a valid NTFS. Maybe the wrong device is used? Or the whole disk instead of a partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?   And the exact same message for /dev/sda1  How can I recover my data from those HDDs?  EDIT     I have tried to specify the type of the drive:  $ sudo mount -t ext4 /dev/sda1 /olddisk mount: wrong fs type, bad option, bad superblock on /dev/sda1,        missing codepage or helper program, or other error        In some cases useful info is found in syslog - try        dmesg | tail  or so $ sudo mount -t ntfs /dev/sdb2 /olddisk NTFS signature is missing. Failed to mount '/dev/sdb2': Invalid argument The device '/dev/sdb2' doesn't seem to have a valid NTFS. Maybe the wrong device is used? Or the whole disk instead of a partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?   EDIT     sudo ntfsfix /dev/sdb2 returns:  Mounting volume... NTFS signature is missing. FAILED Attempting to correct errors... NTFS signature is missing. FAILED Failed to startup volume: Invalid argument NTFS signature is missing. Trying the alternate boot sector Unrecoverable error Volume is corrupt. You should run chkdsk. Which are safe and unsafe regions in Iran? Now there's a lot of propaganda in the west that leads us to believe Iran is dangerous and travellers shouldn't go there.  Obviously there are major political disagreements between several governments and the Iranian government and this should be taken into account.  But at a human level I have met many Iranians travelling elsewhere in the world and have found them to be friendly, charming, and hospitable. I have also met several western travellers who spent time in Iran, including hitchhiking around the country, who only have wonderful things to say about the country and its people.  As a traveller I've learned to trust the most what I hear from other travellers about a destination, especially ones which prefer similar ways of travel to myself. I've also learned to trust the opinions of inhabitants of countries bordering on the destination country the least.  Yet some people still insist that Iran is not safe for a traveller, or that .  I can look up for myself the general travel advisories issued by governments and disclaimers in WikiVoyage. So please don't regurgitate those an answers.  I want to know from travellers who've been there, from Iranians, or from references to specific incidents targeting foreigners/westers about which parts of Iran are no problem, and which parts should be avoided, even by experienced intrepid travellers.  (I realize any advice won't apply to Israeli citizens unfortunately. I'm Australian.) Rounding Percentages Why do Lagrange Multipliers work? I know that the Lagrange multiplier method helps us evaluate critical points of $f$ on the closed boundary of the restriction.   In other words we solve:$$\nabla f=\lambda \nabla g$$  But why does this method actually work? Can someone please give me an explanation. Thank you! Parsing JSON from URL How can I show notify-send messages triggered by crontab? How can I execute an equivalent of `head -z` when I don't have the `-z` option available? Securely erase hard drive using the Disk Utility What is the rigor behind U subsitution? $ \int f(g(x)) dx = \int \frac {f(u)}{u'} du$  requires that  $ \int f(x) dx = \int f(x) \cdot dx$  but dx just represents the variable that F(x) +c is a function of. So why is it legal for dx to be treated algebriacally?  I tried investigating this property by using riemann summation:  $\lim\limits_{n \to \infty}(\sum_{k=1}^n f(\frac{kx}{n} )\frac{x}{n}) = \int_0^x f(t)dt= (F(x)+c)-(F(0)+c)$  and so you can define   $ \lim\limits_{n \to \infty} (\sum_{k=1}^n f(\frac{kx}{n} )\frac{x}{n})+F(0)+c)  = \int f(x) dx$  you can write $\frac{x}{n} = dx$ and $k\frac{x}{n}=kdx= x_k$  then you have  $ \lim\limits_{n \to \infty} (\sum_{k=1}^n f(x_k)dx)+F(0)+c) = \int f(x) dx$  since  dx is being written to be multiplied by the series, then you can define dx in different terms to aquire an integral in terms of other variables.   but that exists only in abstraction. I can't quite sufficiently complete the task of doing so. Renormalized mass I am reading Schwarz QFT and I reached the mass renormalization part. So he introduces, after renormalization, a physical mass, defined as the pole of the renormalized propagator, and a renormalized mass which can be the physical mass, but can also take other values, depending on the subtraction schemes used. Are these masses, other than the physical one, observable in any way experimentally, or are they just ways to do the math easier (using minimal subtraction instead of on-shell scheme, for example)?   Also, in the case of charge renormalization, the explanation was that due to the vacuum polarization, the closer you are to the charge, the more you see of it, so the charge of the particle increases with the momentum you are testing the particle with. However, I am not sure I understand, from a physical point of view, why do you need to renormalize the mass. Is this physical mass (defined as the pole of the renormalized propagator) the same no matter how close you get, or it also changes with energy? And if it changes, what is shielding it at big distances? I know that the emergency services can be called FREE in the UK by dialing 999 and in the US by dialing 911.   What I don't know is the number to call in other countries and if there is a cost involved. This is something I think most people should find out before travelling but many don't (including me). Hopefully this will help me and other people. I was wondering how to run a command as another user from a script.  I have the script's owner set as root. I also have the following command being run within the script to run the command as the hudson user:  su -c command hudson   Is this the correct syntax? Well, the original task was to figure out what the following expression evaluates to for any $n$.  $$\begin{pmatrix} 1 &amp; 1 \\ 0 &amp; 1\end{pmatrix}^{\large n}$$  By trying out different values of $n$, I found the pattern: $$\begin{pmatrix} 1 &amp; 1 \\ 0 &amp; 1\end{pmatrix}^{\large n} = \begin{pmatrix} 1 &amp; n \\ 0 &amp; 1\end{pmatrix}$$  But I have yet to figure out how to prove it algebraically.   Suggestions? command to zip multiple directories into individual zip files I have a single directory that contains dozens of directories inside of it.  I'm new to command line and I'm struggling to come up with a command that will zip each sub-directory into a unique sub-directory.zip file.  So in the end my primary directory will be filled with all of my original sub-directories, plus the corresponding .zip files that contain the zipped up content of each sub-directory.  Is something like this possible? If so, please show me how it's done. Should I defragment my SSD? I believe that Asimov himself recommended reading them in chronological order, however I read them in the order that they were published and think that this is the best way to do it.  What is the correct order? Lets say we have a dependent variable $Y$ with few categories and set of independent variables.    What are the advantages of multinomial logistic regression over set of binary logistic regressions (i.e. )? By set of binary logistic regression I mean that for each category $y_{i} \in Y$ we build separate binary logistic regression model with target=1 when $Y=y_{i}$ and 0 otherwise. What is the reason behind the multiple Spider-Man reboots?