How to convert a file into a dictionary?
d = {} with open(“file.txt”) as f: for line in f: (key, val) = line.split() d[int(key)] = val
d = {} with open(“file.txt”) as f: for line in f: (key, val) = line.split() d[int(key)] = val
To process a file line-by-line, you simply need to decouple the reading of the file and the code that acts upon that input. You can accomplish this by buffering your input until you hit a newline. Assuming we have one JSON object per line (basically, format B): var stream = fs.createReadStream(filePath, {flags: ‘r’, encoding: ‘utf-8’}); … Read more
If you mean File.separator and File.pathSeparator then: File.pathSeparator is used to separate individual file paths in a list of file paths. Consider on windows, the PATH environment variable. You use a ; to separate the file paths so on Windows File.pathSeparator would be ;. File.separator is either / or \ that is used to split … Read more
While not a direct solution, and also bad in that it only (as far as I’ve tested) works with onfocus (requiring a pretty limiting event blocking) you can achieve it with the following: document.body.onfocus = function(){ /*rock it*/ } What’s nice about this, is that you can attach/detach it in time with the file event, … Read more
If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. In your case, the file test.rtf … Read more
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
Just get a handle to the root logger and add the StreamHandler. The StreamHandler writes to stderr. Not sure if you really need stdout over stderr, but this is what I use when I setup the Python logger and I also add the FileHandler as well. Then all my logs go to both places (which … Read more
Below is one way to upload files, there are many other ways. As @nordenheim said, $HTTP_POST_FILES has been deprecated since PHP 4.1.0, thus not advisable to use so. PHP Code (upload.php) <?php $target_dir = “upload/”; $target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]); $uploadOk = 1; $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); // Check if image file is a actual … Read more
I had the same problem as described here. I want to look at lines in file in reverse order, from the end back to the start (The unix tac command will do it). However my input files are fairly large so reading the whole file into memory, as in the other examples was not really … Read more
You could run some jQuery client-side validation to check: $(function(){ $(“input[type=”submit”]”).click(function(){ var $fileUpload = $(“input[type=”file”]”); if (parseInt($fileUpload.get(0).files.length)>2){ alert(“You can only upload a maximum of 2 files”); } }); }); http://jsfiddle.net/Curt/u4NuH/ But remember to check on the server side too as client-side validation can be bypassed quite easily.