I have kept a good number of files in a folder. Now I want to read all of them.  They are in different formats and different encoding. Using listdir/glob.glob I am able to find the list but  how to open/read or process them for different encodings?  If any one can help me out.I am using Python3.2 on Windows.  Regards, Subhabrata Banerjee.
5093
Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.  Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) header in the HTTP response.  Based on that assumption, in addition to the bytes of the file, you also want to save the MIME type.  How would you find the MIME type of a file? I'm currently on a Mac, but this should also work on Windows.   Does the browser add this information when posting the file to the web page?  Is there a neat python library for finding this information? A WebService or (even better) a downloadable database?
21512
I received some text that is encoded, but I don't know what charset was used. Is there a way to determine the encoding of a text file using Python?  deals with C#.
94748
How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode?
19905
I have a bunch of folders which are labelled in this way:   conf1  conf2  ...   But the order in the home directory is like   conf1  conf10  conf100  conf101  ...  conf2  conf20  conf200  conf201  ...   Because each folder contains a file named "distance.txt", I would like to be able to print the content of the distance.txt file, from each single folder, but in order, going from folder 1-->2-->3... to the final folder 272.   I tried several attempts, but every time the final file contains the all set of values in the wrong order; this is the piece of code I set:     ls -v | for d in ./*/;       do (cd "$d" &amp;&amp; cat distance.txt &gt;&gt; /path/to/folder/d.txt          );      done   As you can see I tried to "order" the folders with the command  ls -v   and then to couple the cycle to iteratively save each file.  Can you kindly help me?
103997
I'm in a folder: /var/myfolder.   Inside there are some other folders like: /var/myfolder/A/ /var/myfolder/B/ `/var/myfolder/C/ etc.   Inside each there are some files with random names. How do I remove all the files from all the folders inside /var/myfolder?   The structure (all the directories, eg., A, B, C etc., inside /var/myfolder) should remain intact.
16296
Whenever I list the contents of a directory with a function like readdir, the returned file names also include "." and "..". I have the suspicion that these are just normal links in the file system and therefore indistinguishable from actual files, but I always have to filter them out because they are not actual objects in the directory I am listing. Is there a good reason for functions like readdir to include them? Do some operating systems or file systems contain more or different virtual file names? Is there a better way to filter them out other than by doing string comparison with "." and ".."?  Update: thank you all for answering. I suppose I always thought that things like ./ and ../ were mere conventions that could be handled by searching and replacing. I find it a bit surprising, though probably more efficient and transparent, to have them be part of the file system itself.  One question remains, though: since . and .. are arbitrary names for these links, are there file systems that use different ones?
29750
I have a folder ExampleFolder which contains many zipped folders. These zipped folders also contains multiple zipped folders. How to go through ExampleFolder and extract all of its zipped folders recursively?   Note that ExampleFolder itself, is not zipped, it's just a normal folder  Okay now I know what's happening Rinzwind and nux answers are both correct (except that I have to keep pressing Y or A forever)  But it's seems that some of the zipped files, contained zipped files and rar files as well, the rar files will not be extracted by   while [ "`find . -type f -name '*.zip' | wc -l`" -gt 0 ]; do find -type f -name "*.zip" -exec unzip -- '{}' \; -exec rm -- '{}' \;; done    or whatever  Edit 2 Some files are named .r00 and r01, .r02 up to r14, they are rar files, but not .rar
99848
Using python 2.4 and the built-in ZipFile library, I cannot read very large zip files (greater than 1 or 2 GB) because it wants to store the entire contents of the uncompressed file in memory.  Is there another way to do this (either with a third-party library or some other hack), or must I "shell out" and unzip it that way (which isn't as cross-platform, obviously).
97364
I would like to know how file types are known if filenames don't have suffixes.  For example, a file named myfile could be binary or text to start with, how does the system know if the file is binary or text?
35461
I'd like to write something like this:  $ ls **.py   in order to get all .py filenames, recursively walking a directory hierarchy.  Even if there are .py files to find, the shell (bash) gives this output:  ls: cannot access **.py: No such file or directory   Any way to do what I want?  EDIT: I'd like to specify that I'm not interested in the specific case of ls, but the question is about the glob syntax.
75383
For some crazy reason I can't find a way to get a list of files with a glob for a given directory.  I'm currently stuck with something along the lines of:  NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; NSArray *dirContents = [[NSFileManager defaultManager]                          directoryContentsAtPath:bundleRoot];   ..and then stripping out the stuff I don't want, which sucks.  But what I'd really like is to be able to search for something like "foo*.jpg" instead of asking for the entire directory, but I've not been able to find anything like that.  So how the heck do you do it?
5845
I tried to use java.io.FileReader to read some text files and convert them into a string, but I found the result is wrongly encoded and not readable at all.  Here's my environment:   Windows 2003, OS encoding: CP1252 Java 5.0   My files are UTF-8 encoded or CP1252 encoded, and some of them (UTF-8 encoded files)  may contain Chinese (non-Latin) characters.  I use the following code to do my work:     private static String readFileAsString(String filePath)     throws java.io.IOException{         StringBuffer fileData = new StringBuffer(1000);         FileReader reader = new FileReader(filePath);         //System.out.println(reader.getEncoding());         BufferedReader reader = new BufferedReader(reader);         char[] buf = new char[1024];         int numRead=0;         while((numRead=reader.read(buf)) != -1){             String readData = String.valueOf(buf, 0, numRead);             fileData.append(readData);             buf = new char[1024];         }         reader.close();         return fileData.toString();     }   The above code doesn't work. I found the FileReader's encoding is CP1252 even if the text is UTF-8 encoded. But the JavaDoc of java.io.FileReader says that:     The constructors of this class assume   that the default character encoding   and the default byte-buffer size are   appropriate.   Does this mean that I am not required to set character encoding by myself if I am using FileReader? But I did get wrongly encoded data currently, what's the correct way to deal with my situtaion? Thanks.
96996
Suppose that I have a directory with thousands of files; how could I open all files whose name contains both the strings "red" and "green"?
50127
What would be the most efficient method of reading a text file into a dynamic one-dimensional array? reallocing after every read char seems silly, reallocing after every read line doesn't seem much better. I would like to read the entire file into the array. How would you do it?
37773
I wrote a bash script that processes a list of filenames (using glob expansion, as in for f in *) and then outputs a subset of this list to a file. Subsequently I read the content of this file into an array and perform a plain binary search for specific filenames using the obvious &lt; and &gt; operators for comparing strings.  Considering that I would like the script to work on as many different environments like Linux, MacOS, MinGW, ... (even though it uses things like [[ and stat which are less portable), my questions are:   Do I need to sort the file content (either with sort or additional bash code) or is the glob expansion always sorted - in every environment? Do the conditional operators use the same "sorting" as the expansion (or after sort)?  Would expansion or sort return file10.txt after file2.txt (in what cases?) but using conditional operators file10.txt would be before file2.txt ? What sort option would I  use to fix this? Are there any caveats if some of my filenames are in Unicode?   Are there any issues using specific versions of bash?   Does LC_COLLATE affect any of the above?     I obviously need the file content to match the sorting "method" of the operators in order for the binary search to work as expected...
25881
I want to see the contents (list of files and folders) of an archive, for example a  tar.gz file without extracting it.  Are there any methods for doing that?
81942
In the past, I learned that in Linux/UNIX file systems, directories are just files, which contain the filenames and inode numbers of the files inside the directory.   Is there a simple way to see the content of a directory? I mean the way the files names and inodes are stored/organized.  I'm not looking for ls, find or something similiar. I also don't want to see the content of the files inside a directory. I want to see the implementation of the directories. If every directory is just a text file with some content, maybe a simple way exists to see the content of this text file.  In the bash in Linux it is not possible to do a cat folder. The output is just Is a directory.  Update The question  addresses the same issue but it has no helpful solution like the one from .