Wait until file is unlocked in .NET

Starting from Eric’s answer, I included some improvements to make the code far more compact and reusable. Hope it’s useful. FileStream WaitForFile (string fullPath, FileMode mode, FileAccess access, FileShare share) { for (int numTries = 0; numTries < 10; numTries++) { FileStream fs = null; try { fs = new FileStream (fullPath, mode, access, share); … Read more

How to filter files when using scp to copy dir recursively?

I’d probably recommend using something like rsync for this due to its include and exclude flags, e.g:- rsync -rav -e ssh –include ‘*/’ –include=”*.class” –exclude=”*” \ server:/usr/some/unknown/number/of/sub/folders/ \ /usr/project/backup/some/unknown/number/of/sub/folders/ Some other useful flags: -r for recursive -a for archive (mostly all files) -v for verbose output -e to specify ssh instead of the default (which … Read more

open() gives FileNotFoundError/IOError: Errno 2 No such file or directory

Make sure the file exists: use os.listdir() to see the list of files in the current working directory Make sure you’re in the directory you think you’re in with os.getcwd() (if you launch your code from an IDE, you may well be in a different directory) You can then either: Call os.chdir(dir), dir being the … Read more

Check if object is file-like in Python

For 3.1+, one of the following: isinstance(something, io.TextIOBase) isinstance(something, io.BufferedIOBase) isinstance(something, io.RawIOBase) isinstance(something, io.IOBase) For 2.x, “file-like object” is too vague a thing to check for, but the documentation for whatever function(s) you’re dealing with will hopefully tell you what they actually need; if not, read the code. As other answers point out, the first … Read more

What should a Multipart HTTP request with multiple files look like? [duplicate]

Well, note that the request contains binary data, so I’m not posting the request as such – instead, I’ve converted every non-printable-ascii character into a dot (“.”). POST /cgi-bin/qtest HTTP/1.1 Host: aram User-Agent: Mozilla/5.0 Gecko/2009042316 Firefox/3.0.10 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://aram/~martind/banner.htm Content-Type: multipart/form-data; boundary=2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f Content-Length: 514 … Read more

Batch files: How to read a file?

Under NT-style cmd.exe, you can loop through the lines of a text file with FOR /F %%i IN (file.txt) DO @echo %%i Type “help for” on the command prompt for more information. (don’t know if that works in whatever “DOS” you are using)