How to extract a zip file using python when zip file present in different directory where script file present.
I try this ,but i got error because source path is not accepted ,try to solve me this problem.
from zipfile import ZipFile
def func(source, target):
with ZipFile('source', 'target'):
ZipFile.Extractall('target')
Use this code. To move through directories you could either hard code the directory where your script is present or you could toggle through the directories using simple commands such as "../" to move out of the given directory or "/" to move inside a folder in the directory. For example - "../script.py" or "/folder/script.py". Similarly you can use this to find your .zip file.
import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
zip_ref.extractall("targetdir")
For just unpacking, shutil should suffice:
import shutil
shutil.unpack_archive('path-to-zipfile')
You'll have to check for source path of the zip file which is relative to your current working directory. To know your current working directory you can try
import os
print(os.getcwd())
zip - Unzipping files in python
relative-paths-in-python
Related
I'm using Windows 10
Anyway I used os.rename and forgot to add a directory path to the beginning. Where did my files end up?
The file could be sent to the current working directory. To find the current working directory, use
import os
os.getcwd()
To find out where the file you are executing is located, use
import os
os.path.dirname(os.path.realpath(__file__))
I have a file with a function. I want to import the file and when doing that I get No module named ex25.
import ex25
I have checked some tutorials and the offial documentation.
import ex25
No module named ex25
First, check if the file is .py file and in the same folder with the file that you are currently working on, there shouldn't be any problem at all.
If not in the same folder, but in a different folder and make sure that your file is .py file, you need to make that folder (which contains the file) to a package by adding a file name __init__.py
I have downloaded a csv file(ABC) using python and selenium and need to change the name of the file.
Input= ABC.csv #downloaded file with name ABC
Output= DEF.csv #New file with name DEF.
Any help is appreciated.
try this
import os
os.rename('a.txt', 'b.kml')
From:
How to rename a file using Python
Thanks All.. Used the below code and it is working fine. All others kept throwing the error as File not found. Used complete file path.
os.rename('C:\\Users\\pathname\\ABC.csv', 'C:\\Users\\pathname\\DEF.csv')
You can use os module to rename the file:
import os
os.rename('path to ABC.csv', 'path to DEF.csv')
Explanation:
rename first argument is the original file which needs to be renamed and second argument is the new name which it should be renamed. If the files are in current directory it will work by simply using the file names. Otherwise include the path where these files are there
More information at docs **https://docs.python.org/3/library/os.html#os.rename
How can we get the compiled python pyc file path with python 3.x in the current environment? I know it's in the __pycache__ direcotry, but I couldn't find a way to find the file path. Because the names of pyc files of python 3 changes by the environment.
Given that you know the path to the source (ie .py) file, there's a function importlib.util.cache_from_source that does exactly what you want. For example, to get the .pyc file corresponding to the numpy package, you would do:
import importlib
import numpy
importlib.util.cache_from_source(numpy.__file__)
For me (on OS X), this prints out something along the lines of:
/<absolute path to site-packages>/numpy/__pycache__/__init__.cpython-36.pyc
I follow some articles regarding on how to list files in a directory using the below code :
import os, sys
path = "/python 3.6.4"
dirs = os.list(path)
for file in dirs:
print file
Note: The .py file in which the above code is on the same directory as the path.
Im running it using a command prompt. Any idea would be much appreciated. Thanks!