Rectangle 27 17

first open a file:

with open("filename") as fileobj:
    for line in fileobj:  
       for ch in line: 
           print ch

why isn't this the correct answer?

Agreed, this seems like the more Pythonic way to go about it. Wouldn't this take care of handling non-ASCII encoding as well?

How to read a single character at a time from a file in Python? - Stac...

python file-io character
Rectangle 27 178

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 178

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 175

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 175

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 175

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 175

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 175

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 175

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

Add a file called .bashrc to your home folder.

Open the file and paste in:

#!/bin/bash
eval `ssh-agent -s`
ssh-add

This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

Add to or create file ~/.ssh/config with the contents

ForwardAgent yes

In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

When I do eval `ssh-agent -s` the process does not stop when I exit cygwin.

Is there a way to setup the config for Windows / gitbash so you dont have to do it every time?

It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs.

ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting.

ssh - Could not open a connection to your authentication agent - Stack...

ssh ssh-agent
Rectangle 27 3319

If the reason you're checking is so you can do something like if file_exists: open_it(), it's safer to use a try around the attempt to open it. Checking and then opening risks the file being deleted or moved or something between when you check and when you try to open it.

If you're not planning to open the file immediately, you can use os.path.isfile

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

import os.path
os.path.isfile(fname)

if you need to be sure it's a file.

Starting with Python 3.4, the pathlib module offers an object-oriented approach (backported to pathlib2 in Python 2.7):

from pathlib import Path

my_file = Path("/path/to/file")
if my_file.is_file():
    # file exists

To check a directory, do:

if my_file.is_dir():
    # directory exists

To check whether a Path object exists independently of whether is it a file or directory, use exists():

if my_file.exists():
    # path exists

You can also use resolve() in a try block:

try:
    my_abs_path = my_file.resolve():
except FileNotFoundError:
    # doesn't exist
else:
    # exists

The official backport is pathlib2, pathlib module isn't maintained anymore.

os.path.isfile(file_path) and Path(str(file_path)).is_file()
os
pathlib

I totally agree that there is little reason to simply check for presence of a file unless the mere presence of it is a signal all by itself. In which case it would be better to have a CSV file where you write a value indicating the signal - and then can have more than one signal conveyed via a single file.

Good point with the possibility of the file being changed after the check!

How do I check whether a file exists using Python? - Stack Overflow

python file filesystems
Rectangle 27 3301

If the reason you're checking is so you can do something like if file_exists: open_it(), it's safer to use a try around the attempt to open it. Checking and then opening risks the file being deleted or moved or something between when you check and when you try to open it.

If you're not planning to open the file immediately, you can use os.path.isfile

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

import os.path
os.path.isfile(fname)

if you need to be sure it's a file.

Starting with Python 3.4, the pathlib module offers an object-oriented approach (backported to pathlib2 in Python 2.7):

from pathlib import Path

my_file = Path("/path/to/file")
if my_file.is_file():
    # file exists

To check a directory, do:

if my_file.is_dir():
    # directory exists

To check whether a Path object exists independently of whether is it a file or directory, use exists():

if my_file.exists():
    # path exists

You can also use resolve() in a try block:

try:
    my_abs_path = my_file.resolve():
except FileNotFoundError:
    # doesn't exist
else:
    # exists

The official backport is pathlib2, pathlib module isn't maintained anymore.

os.path.isfile(file_path) and Path(str(file_path)).is_file()
os
pathlib

I totally agree that there is little reason to simply check for presence of a file unless the mere presence of it is a signal all by itself. In which case it would be better to have a CSV file where you write a value indicating the signal - and then can have more than one signal conveyed via a single file.

Good point with the possibility of the file being changed after the check!

How do I check whether a file exists using Python? - Stack Overflow

python file filesystems
Rectangle 27 60

You don't have to open the file, but instead, you have to create a ReadStream.

fs.createReadStream
Lazy

Is there something like an end event for Lazy? When all lines have been read in?

@Cecchi and @Max, don't use join because it will buffer the entire file in memory. Instead, just listen to the 'end' event: new lazy(...).lines.forEach(...).on('end', function() {...})

@Cecchi, @Corin, and @Max: For what it's worth, I drove myself crazy chaining .on('end'... after .forEach(...), when in fact everything behaved as expected when I bound the event first.

This result is very high on search results, so it is worth noting that Lazy looks abandoned. It has been 7 months without any changes, and has some horrifying bugs (last line ignored, massive memory leaks, etc).

javascript - Read a file one line at a time in node.js? - Stack Overfl...

javascript node.js file-io lazy-evaluation
Rectangle 27 6

How to indent highlighted code in vi immediately by a # of spaces:

Option 1: Indent a block of code in vi to three spaces with Visual Block mode:

Option 2: Indent a block of code in vi to three spaces with Visual Line mode:

Vjjjj:le 3

Interpretation of what you did:

: tells vi you will enter an instruction for the highlighted text.

le 3

The selected code is immediately increased or decreased to three spaces indentation.

Option 3: use Visual Block mode and special insert mode to increase indent:

  • Open your file in VI.

Be in normal mode press the following keys:

jjjj

All the highlighted text is indented an additional 5 spaces.

vim - Indent multiple lines quickly in vi - Stack Overflow

vim editor indentation vi
Rectangle 27 6

How to indent highlighted code in vi immediately by a # of spaces:

Option 1: Indent a block of code in vi to three spaces with Visual Block mode:

Option 2: Indent a block of code in vi to three spaces with Visual Line mode:

Vjjjj:le 3

Interpretation of what you did:

: tells vi you will enter an instruction for the highlighted text.

le 3

The selected code is immediately increased or decreased to three spaces indentation.

Option 3: use Visual Block mode and special insert mode to increase indent:

  • Open your file in VI.

Be in normal mode press the following keys:

jjjj

All the highlighted text is indented an additional 5 spaces.

vim - Indent multiple lines quickly in vi - Stack Overflow

vim editor indentation vi
Rectangle 27 171

You opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'.

@Roberto: you do realise that that is a joke PEP, right?

The part i linked is NOT a joke though ;)

Sure, but there are perhaps better documents to link to, on the whole. :-) Like the porting guide.

@user877329: no, the bytes object is doing the containment check; essentially bytes.__contains__(str) is executed, and it is the bytes object that then complains about a string being passed in.

string - python 3.5: TypeError: a bytes-like object is required, not '...

python string python-3.x byte
Rectangle 27 158

You opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'.

@Roberto: you do realise that that is a joke PEP, right?

The part i linked is NOT a joke though ;)

Sure, but there are perhaps better documents to link to, on the whole. :-) Like the porting guide.

@user877329: no, the bytes object is doing the containment check; essentially bytes.__contains__(str) is executed, and it is the bytes object that then complains about a string being passed in.

string - python 3.5: TypeError: a bytes-like object is required, not '...

python string python-3.x byte
Rectangle 27 33

You can use the following code to open a file location from vba.

Dim Foldername As String
Foldername = "\\server\Instructions\"

Shell "C:\WINDOWS\explorer.exe """ & Foldername & "", vbNormalFocus

You can use this code for both windows shares and local drives.

VbNormalFocus can be swapper for VbMaximizedFocus if you want a maximized view.

Thanks so much for posting this - it is EXACTLY what I needed and I love the fact that Jeff encourages folks to ask and answer in order to supply stuff like this.

Shell "explorer """ & vFolderPath & "", vbNormalFocus

The problem with this approach is that if you click it again, it opens another Explorer window. The only way that I can fix this and get it to switch to an existing Explorer window if there is one open at the right location is, "cmd.exe /C start " & Foldername. This has the down-side of flashing up a Command Prompt window.

ms access - How to open a folder in Windows Explorer from VBA? - Stack...

ms-access vba
Rectangle 27 4

printf( Couldn't open the file.\n" );

with

perror(argv[1]);

then, simply compile your program in your terminal, e.g. type there a shell command similar to

-Wextra
-g
gdb

assuming your source code is in a file named mysource.c in the current working directory (use pwd command to query that current directory, ls -al to list it, and cd builtin command to change it)

at last, run your program as

./myprog sometestfile.txt

You might want to use the debugger. Read first about GDB, and try perhaps

gdb ./myprog

and using GCC compiler

PATH

Later, you'll want to have some bigger program of yours made in several translation units, having some common header file (to be #included in all of them). Then you'll use a builder like GNU make. You could use a good editor like emacs, some version control like git, etc...

PS. Perhaps replace printf( "%c", x ); with the shorter and more efficient putchar(x); ...

gcc -Wall -g mysource.c -o myprog
-Wall

-Wall ("warnings all") indicates a high warning level. (It doesn't include all possible warnings, though.) Please make it a habit to compile with -Wall. It will help you to catch many errors, for example a mismatch of printf formats and arguments.

gcc -Wall -g main.c -o CppApplication_1
gcc: error: main.c: No such file or directory

Because you are not in the directory containing main.c; you should use some cd command to go there

@BasileStarynkevitch @MOehm If I had the CppApplication_1 on `C:` how I'd put the commands?

c - About `argv` and opening a file with command line - Stack Overflow

c argv argc
Rectangle 27 87

If you're on Python 2.5 or earlier, open the file and then use the file object in your redirection:

log = open("c:\\goat.txt", "w")
print >>log, "test"

If you're on Python 2.6 or 2.7, you can use print as a function:

from __future__ import print_function
log = open("c:\\goat.txt", "w")
print("test", file = log)

If you're on Python 3.0 or later, then you can omit the future import.

import sys
sys.stdout = open("c:\\goat.txt", "w")
print ("test sys.stdout")

@BenBlumer: That's a good point; you should only do the future import of the print function in a file where you're willing to convert ALL of your print statements to function calls.

sys.stdout = sys.__stdout__

printing - Python How to simply redirect output of print to a TXT file...

python printing redirect
Rectangle 27 49

Open the file as binary to prevent the translation of end-of-line characters:

f = open('file.txt', 'wb')

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but itll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesnt hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

newline - How to write Unix end of line characters in Windows using Py...

python newline