[PYTHON] Scan all your local directory files

Scan local files in directory

At certain times, we may need to perform scanning for all the files stored in local directory. We can perform so using the code as follows:


import os
    # scan all files in directory
    for file in os.listdir('.'):
        if file.endswith('.json'):
             # perform action here
             print(file) # for testing purpose, we are merely print the file names

However, printing the file names may not be enough. Let’s try to perform some further actions: read and scan all the json files in the local directory, combine (remove duplicate) and print it out!


import os
import json

# defining the lists
all_json = []
    # scan all files in directory
    for file in os.listdir('.'):
        if file.endswith('.json'):
            # action starts here
            with open(os.path.abspath('') + '/' + file) as f:
                # to union: convert the list into set
                # and uses "|" to UNION it
                all_json = list(set(all_json) | set(json.load(f)))
                # Sort the lit
                all_json.sort()

# print the combined jsons
print(all_json)

[Android] Fetch Data using URL Link (JSON/XML)

As usual, before getting into this tutorial, you need to know how an android application code works and how different important file interact with each other (To be updated)

1. Insert the following into your Manifest.xml (AndroidManifest.xml, understand the tab) to enable the permissions to access internet

 <!-- Adding for accessing internet -->
 <uses-permission android:name="android.permission.INTERNET" />

2. Create the following method to read Data from internet

/**
     * Get Data, based on url input
     * @param urlPath path linked to the data
     * @return null if not data is found, or String if there is data
     */

    public static String readURL(String urlPath) {
        // These two need to be declared outside the try/catch
        // so that they can be closed in the finally block.
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        // Will contain the raw text response as a string.
        String textStr = null;
        try {
            // Construct the URL based on the link provided
            URL url = new URL(urlPath);

            // Create the request to the link provided
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                // Nothing to do.
                textStr = null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;

            // Reading the strings line by line and append new line (\n)
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                // Stream was empty. No point in parsing.
                textStr = null;
            }
            textStr = buffer.toString();
        } catch (IOException e) {
            Log.e("PlaceholderFragment", "Error ", e);
            // If the code didn't successfully get the string, there's no point in attempting
            // to parse it.
            textStr = null;
        } finally{
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e("PlaceholderFragment", "Error closing stream", e);
                }
            }
        }

        return textStr;
    }

3. Then you can call the above method, which will attempt to read the data based on the link that you have provided! Android development recommends the usage of Thread to retrieve data so that the operation of application is not interrupted by the data retrieve process. (To be updated)

4. That’s all! Try to code it now!

Reference:
1. Connecting to the Network, Android Development: http://developer.android.com/training/basics/network-ops/connecting.html
2. Data retrieve methods from github: https://github.com/stilva/udacity-dev-android/blob/master/app/src/main/java/com/stilva/android/sunshine/app/FetchWeatherTask.java