I want to search for a line in a file and then substitute that line for another. I've found this question:  In the main question the user asks for a replace function, that works like this:  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   , which is exactly what i need but i can't seem to get it to work. Can someone maybe explicitly use the replace function given in the answer for the problem above and write the code down? (I don't know what to put for "file_path" in the replace function. Is there some way to just tell python to use the current directory or something like that?)  Sorry if it is a trivial question but I've been strugling with it for quite some time now and i can't get it to work. Thanks in advance!
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
If you willingly fall on your turn does it cost movement?  As a DM I'm thinking about how to handle things such as creatures who are immune to being prone, diving into the water from a height, or a flying creature diving off a cliff and taking a fall before taking flight. There are many circumstances where you can fall without taking damage or being knocked prone afterward, and I'm not sure how to determine movement following the dive.  For example, my character walks up to a ledge above a 10 foot drop to the ground below. After taking 5 feet of movement to step off the ledge, I allow myself to fall. When I hit the ground have I used 15 feet of movement or just 5? Does it make sense to combine PCA and LDA? Assume I have a dataset for a supervised statistical classification task, e.g., via a Bayes' classifier. This dataset consists of 20 features and I want to boil it down to 2 features via dimensionality reduction techniques such as Principal Component Analysis (PCA) and/or Linear Discriminant Analysis (LDA).  Both techniques are projecting the data onto a smaller feature subspace: with PCA, I would find the directions (components) that maximize the variance in the dataset (without considering the class labels), and with LDA I would have the components that maximize the between-class separation.  Now, I am wondering if, how, and why these techniques can be combined and if it makes sense.  For example:   transforming the dataset via PCA and projecting it onto a new 2D subspace transforming (the already PCA-transformed) dataset via LDA for max. in-class separation   or    skipping the PCA step and using the top 2 components from a LDA.   or any other combination that makes sense. How do I write a good title? A good title helps your question get the attention it deserves.  What goes into a good title? Why is triple-DES using three different keys vulnerable to a meet-in-the-middle-attack? Among other sources,  states that triple DES using three seperate keys (k1, k2, k3) is vulnerable to meet-in-the-middle-attacks, while triple DES using only two keys (k1, k2, k1) is not.  Why is this attack possible if all the keys are different, but not if the first and the third are the same? Let $f$ be a continuous function on $[0,1]$ such that $f([0,1])=[0,1]\times[0,1].$ Then show that $f$ is not one-one.  Hints will be appreciated. Should an off-topic question always be downvoted? The upvote tooltip says:      This question shows research effort; it is useful and clear.   The downvote tooltip says:      This question does not show any research effort; it is unclear or not useful.   Sometimes, a question can show research effort, be useful, be clear, but just not in the good place. Voting to close the question and downvoting seems like a double penalty.  Should an off-topic question always be downvoted ? How to set page geometry for a single page only? I'm looking to take a spontaneous trip for a long weekend. The date is flexible and the destination can be anywhere in the world. Is there a site that would allow me to search airfares within a date range, while keeping the destination parameters open? Allow applications downloaded from anywhere in macOS Sierra In previous versions of the OS in the security pane, you are provided an option of:    But in this version there is no option anywhere:    Is there a way that I can turn on anywhere? (via Terminal maybe?) I'm try to change to my Desktop directory from the terminal.  Running  cd /home/desktop   gives an error in the terminal saying no directory found.  What am I doing wrong? iPhone screen randomly turns on After installing iOS 10, I found that the screen on my iPhone 6s turned on at seemingly random times, and it slightly annoyed me. I now know that this feature does what it is supposed to (turn the screen on when the phone enters certain positions), but I want to disable it.  Is there a setting can I use to do so? Attribution alignment and order should be consistent with the website Scoring when two triple word squares are covered in one play The word QUAGMIRE is on the board vertically, with the Q on the top row, second column. The next player creates SQUEEZED horizontally, landing the S on the triple word square in the top row, first column, and remaining UEEZED landing to the right of the existing Q, ending with the D on the triple word square in the eighth column. Since two triple word squares are covered in this one placement, how would it be scored?  Letters:   S: 1 Q: 10 U: 1 E: 1 x 2 (double letter) E: 1 Z: 10 E: 1 D: 1   27 Total for letters. Do you then multiply by 3 for the first triple word square, and then multiply that result by 3?  27 X 3 = 81, 81 X 3 = 243  Bonus for all 7 tiles placed:  243 + 50 = 293 How to get the file name from a full path using JavaScript? I was working on a userscript today, which had a goal of displaying chat pings immediately at the main site. This would allow me to respond more quicker-like to chat pings.  However, I was met by this fearsome warrior:     Can we please set some decent header things? I want to be able to do this really bad. What's the difference between terms 'link function' and 'canonical link function'? Also, are there any (theoretical) advantages of using one over the other?  For example, a binary response variable can be modeled using many link functions such as logit, probit, etc. But, logit here is considered the "canonical" link function. How do I check which shell I am using? I read that terminal is nothing but shell, and Unix provides different flavors of shells:   Bourne shell (sh) C shell (csh) TC shell (tcsh) Korn shell (ksh) Bourne Again shell (bash)   Questions:   When I open a terminal window, which shell is opened by default? How do I check how many shells are installed? How do I change the shell used from my account? Does anyone know what the Inverse Cumulative Distribution Function of Normal Distribution is? Does it have a closed form expression? I did not find any good answer using Google. Weird Integer boxing in Java How can I find out which process is locking a file or folder in Windows?  For instance, when trying to delete a folder, Windows reports this:     The action can't be completed because the folder is open in another program   Happens the same with a file, but how do I find out what program or application is currently using it and preventing me from deleting the file or folder?