How do i import a file from one file to another in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two text files, the task is to write a Python program to copy contents of the first file into the second file.

    The text files which are going to be used are second.txt and first.txt:

    How do i import a file from one file to another in python?

    Method #1: UsingFile handling to read and append

    We will open first.txt in ‘r’ modeand will read the contents of first.txt. After that, we will open second.txt in ‘a’ mode and will append the content of first.txt into second.txt.

    Example:

    Python3

    with open('first.txt','r') as firstfile, open('second.txt','a') as secondfile:

        for line in firstfile:

                 secondfile.write(line)

    Output:

    How do i import a file from one file to another in python?

    Method #2: UsingFile handling to read and write

    We will open first.txt in ‘r’ modeand will read the contents of first.txt. After that, we will open second.txt in ‘w’ mode and will write the content of first.txt into second.txt.

    Example:

    Python3

    with open('first.txt','r') as firstfile, open('second.txt','w') as secondfile:

        for line in firstfile:

                 secondfile.write(line)

    Output:

    How do i import a file from one file to another in python?

    Method #3: Using shutil.copy() module

    The shutil.copy() method in Python is used to copy the content of the source file to destination file or directory. 

    Example:

    Python3

    import shutil

    shutil.copyfile('first.txt','second.txt')

    Output:

    How do i import a file from one file to another in python?


    In Python, a module is a single unit of Python code that can be imported (loaded and used) by other Python code. A module can contain definitions (like functions and constants), as well as statements that initialize those definitions. Once the module code is written, it can be reused by any script that imports the module.

    A common way to create a Python module is to create a file with a filename that ends in .py, and write the module code in there. If we use a different file extension in the filename, or no extension at all, the standard import statements shown below will not work, and we'll have to use importlib instead, which requires more code (more info below).

    To illustrate standard import usage, let's say we create a file called mymodule.py with the following function definition:

    Now every time we want to write "Hello, world!" to the screen from a Python script, we can simply import this module rather than having to write the message again. It also allows us to change one line of code inside mymodule.py rather than in many different scripts if we ever decide to change the message we want to show in all the scripts that use this function.

    mymodule.py contains the say_hello() function we saw above.

    The name used in the import statement is simply the module's filename without the .py extension at the end.

    Python versions 3.3 and higher allow easy imports of modules in subdirectories of the current script's directory. If you're using a Python version lower than 3.3, you can follow the steps in Import a File in a Different Directory instead.

    In a file system path, we would separate the components of a path using / (Linux, macOS, etc.) or \ (Windows). In a Python import statement, however, we separate the path components using a dot (.).

    where m is any name we choose. We can also import the function directly:

    This works even if there are multiple levels of subdirectories. For example, if we had the following directory structure:

    Now let's say that we move mymodule.py to a directory that is outside of the current directory tree:

    By default, Python looks for files in the same directory (including subdirectories) as the current script file, as well as in other standard locations defined in sys.path. If you're curious what these locations are, you can print out the sys.path variable like this:

    However, if the file we want to import is somewhere else entirely, we'll first have to tell Python where to look by adding search directories to sys.path. In our example, we can write in script.py:

    Note that the path appended to sys.path is an absolute path. If we used a relative path, the path would resolve differently based on the directory from which the user is running the script, not relative to script.py's path.

    To append a directory relative to this script file, you can use __file__ to get the current script's full path and build a full path to the import from there. In script.py we can write:

    Python versions 3.4 and higher provide functionality through the built-in importlib library that allows us to load any file anywhere as a Python module, even if the file's filename does not end in .py (it can have a different file extension, or no file extension at all).

    Notice here that the mymodule filename does not have a file extension. In this case, we can't use a simple import statement to import that file. Instead, we can write in script.py:

    Note that the path passed into SourceFileLoader() is an absolute path. If we used a relative path like ../alpha/beta/mymodule, the path would resolve differently based on the directory from which the user is running the script, not relative to script.py's path.

    If we want to reference a file relative to our current script file's path, we can use __file__ to first get our current script file's path, and then build a full path from there:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    import importlib.machinery
    import importlib.util
    from pathlib import Path
    
    # Get path to mymodule
    script_dir = Path( __file__ ).parent
    mymodule_path = str( script_dir.joinpath( '..', 'alpha', 'beta', 'mymodule' ) )
    
    # Import mymodule
    loader = importlib.machinery.SourceFileLoader( 'mymodule', mymodule_path )
    spec = importlib.util.spec_from_loader( 'mymodule', loader )
    mymodule = importlib.util.module_from_spec( spec )
    loader.exec_module( mymodule )
    
    # Use mymodule
    mymodule.say_hello()
    

    How do you transfer files in Python?

    Steps to Copy a File using Python.
    Step 1: Capture the original path. To begin, capture the path where your file is currently stored. ... .
    Step 2: Capture the target path. Next, capture the target path where you'd like to copy the file. ... .
    Step 3: Copy the file in Python using shutil. copyfile..

    Can files import each other Python?

    Import a File in a Subdirectory (Python 3.3 and Up) Python versions 3.3 and higher allow easy imports of modules in subdirectories of the current script's directory. If you're using a Python version lower than 3.3, you can follow the steps in Import a File in a Different Directory instead.

    How do you copy and paste a file in Python?

    Steps to Copy a File in Python.
    Find the path of a file. We can copy a file using both relative path and absolute path. ... .
    Use the shutil.copy() function. ... .
    Use the os.listdir() and shutil copy() function to copy all files. ... .
    Use copytree() function to copy entire directory..