There are different modes you can open a file with, specified by the mode parameter. These include:
'r' - reading mode. The default. It allows you only to read the
file, not to modify it. When using this mode the file must exist.
'w' - writing mode. It will create a new file if it does no...
The preferred method of file i/o is to use the with keyword. This will ensure the file handle is closed once the reading or writing has been completed.
with open('myfile.txt') as in_file:
content = in_file.read()
print(content)
or, to handle closing the file manually, you can forgo with...
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationFailure);
} else {
console.log("Geolocation is not supported by this browser.");
}
// Function that will be called if the query succeeds
var geolocationSuccess = function(pos) {...
In the event that geolocation fails, your callback function will receive a PositionError object. The object will include an attribute named code that will have a value of 1, 2, or 3. Each of these numbers signifies a different kind of error; the getErrorCode() function below takes the PositionError....
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file:
for line in in_file:
out_file.write(line)
Using the shutil module:
import shutil
shutil.copyfile(src, dst)
The following variables set up the below example:
var COOKIE_NAME = "Example Cookie"; /* The cookie's name. */
var COOKIE_VALUE = "Hello, world!"; /* The cookie's value. */
var COOKIE_PATH = "/foo/bar"; /* The cookie's path. */
var COOKIE_EXPIRES; ...
var expiry = new Date();
expiry.setTime(expiry.getTime() - 3600);
document.cookie = name + "=; expires=" + expiry.toGMTString() + "; path=/"
This will remove the cookie with a given name.
This a Basic example for using the MVVM model in a windows desktop application, using WPF and C#. The example code implements a simple "user info" dialog.
The View
The XAML
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
...
The following snippet encodes the data stored in d into JSON and stores it in a file (replace filename with the actual name of the file).
import json
d = {
'foo': 'bar',
'alice': 1,
'wonderland': [1, 2, 3]
}
with open(filename, 'w') as f:
json.dump(d, f)
git diff
This will show the unstaged changes on the current branch from the commit before it. It will only show changes relative to the index, meaning it shows what you could add to the next commit, but haven't. To add (stage) these changes, you can use git add.
If a file is staged, but was modi...
git diff --staged
This will show the changes between the previous commit and the currently staged files.
NOTE: You can also use the following commands to accomplish the same thing:
git diff --cached
Which is just a synonym for --staged or
git status -v
Which will trigger the verbose sett...
You can upvote (or downvote, if you want...) examples you created.
Although you won't get any reputation for doing so, upvoting your the example will influence the examples' ordering on the page.
Any previous contributors to the examples will get reputation.
On the other hand, you might consider ...
Overview
Checkboxes and radio buttons are written with the HTML tag <input>, and their behavior is defined in the HTML specification.
The simplest checkbox or radio button is an <input> element with a type attribute of checkbox or radio, respectively:
<input type="checkbox&quo...
Python's string type provides many functions that act on the capitalization of a string. These include :
str.casefold
str.upper
str.lower
str.capitalize
str.title
str.swapcase
With unicode strings (the default in Python 3), these operations are not 1:1 mappings or reversible. Most of thes...
str.split(sep=None, maxsplit=-1)
str.split takes a string and returns a list of substrings of the original string. The behavior differs depending on whether the sep argument is provided or omitted.
If sep isn't provided, or is None, then the splitting takes place wherever there is whitespace. Howe...
Python's str type also has a method for replacing occurences of one sub-string with another sub-string in a given string. For more demanding cases, one can use re.sub.
str.replace(old, new[, count]):
str.replace takes two arguments old and new containing the old sub-string which is to be replace...