New answer, powershell
TCP
Get-Process -Id (Get-NetTCPConnection -LocalPort YourPortNumberHere).OwningProcess
UDP
Get-Process -Id (Get-NetUDPEndpoint -LocalPort YourPortNumberHere).OwningProcess
Old answer, cmd
C:\> netstat -a -b
(Add -n to stop it trying to resolve hostnames, which will make it a lot faster.)
Note Dane's recommendation for TCPView. It looks very useful!
-a Displays all connections and listening ports.
-b Displays the executable involved in creating each connection or listening port. In some cases well-known executables host multiple independent components, and in these cases the sequence of components involved in creating the connection or listening port is displayed. In this case the executable name is in [] at the bottom, on top is the component it called, and so forth until TCP/IP was reached. Note that this option can be time-consuming and will fail unless you have sufficient permissions.
-n Displays addresses and port numbers in numerical form.
-o Displays the owning process ID associated with each connection.
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(strict=True)
in a try
block:
try:
my_abs_path = my_file.resolve(strict=True)
except FileNotFoundError:
# doesn't exist
else:
# exists
Best Answer
Without redirection, Luc Vu or Erik Konstantopoulos point out to:
"How to create empty text file from a batch file?" (2008) also points to:
Nomad mentions an original one:
In the same spirit, Samuel suggests in the comments:
It does give an error:
But this error is on stderr. And
>
only redirects stdout, where nothing have been produced.Hence the creation of an empty file.
The error message can be disregarded here. Or, as in Rain's answer, redirected to
NUL
:(Original answer, November 2009)
(
echo ""
would actually put "" in the file! Andecho
without the '.' would put "Command ECHO activated
" in the file...)Note: the resulting file is not empty but includes a return line sequence: 2 bytes.
This discussion points to a true batch solution for a real empty file:
Since here the "string to the right of the equal sign" is empty... the result is an empty file.
The difference with
cd. > filename
(which is mentioned in Patrick Cuff's answer and does also produce a 0-byte-length file) is that this "bit of redirection" (the<nul...
trick) can be used to echo lines without any CR: