Rename a file without changing file extension - python-3.x

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

Related

Where does python send files without a directory path?

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__))

Import module (not file) from a different path

I am trying to import a module (not a file) from a different path.
By module, I mean that I have a folder named Util containing a single file named __init__.py.
Using import Util in a python file on the same level as the Util folder works fine.
But this is not the case when the python file resides elsewhere.
Following this answer, I have this piece of code working:
import sys,os
sys.path.append(os.path.dirname(__file__)+'the relative path to the python file that I want to import')
But it works only when I import a python file, not a python module.
How can I resolve this?
Problem solved:
Due to the fact that I am using Windows, the string os.path.dirname(__file__)+'the relative path to the python file that I want to import' contains a mixture of forward slashes (/) and backward slashes (\).
The simple solution is to ensure that the (OS-dependent) expression os.path.dirname(__file__) contains only forward slashes:
os.path.dirname(__file__).replace('\\','/')
Same goes for the constant part 'the relative path to the python file that I want to import' of course, which one can simply fix manually if needed (I already had this one with forward slashes only).

How do I import a file to execute a function

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

How to extract a zip file using python 3

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

importing a python function from a file in other directory which depends on another file

This is my directory structure
-directory1
|-currentlyWritingCode
-directory2
|-__init__.py
|-helper.py
|-view.ui
The helper.py is a UI from pyqt4 framework. It needs the view.ui file .
it has following code to load the ui data
viewBase, viewForm = uic.loadUiType("view.ui")
now in directory1 in the currently writing code I did this
import directory2.helper
when I run the code in currentlyWritingCode it is throwing an error that
FileNotFoundError: [Errno 2] No such file or directory: 'view.ui'
What to do now?
using python3.5 from anaconda on Centos 7
Use os.path.join(os.path.dirname(os.path.realpath(__file__)),'view.ui') in place of view.ui. This will ensure you correctly reference the folder that the python file lives in, regardless of the code that imports it.
Note: Make sure you have import os with your other imports.
How does this work?
__file__ is an attribute of the module you are importing. It contains the path to the module file. See this answer. However, this path is not necessarily an absolute path. os.path.realpath returns the absolute path (it even follows symlinks if there are any). At this point we have a full path to the module, so we take the path to the directory (os.path.dirname) and join it with the original filename (which we assumed to be relative to the original module and so should be in the aforementioned directory). os.path.join ensures that the correct \ or / is used when constructing a file path so that the code works on any platform.

Resources