Warm tip: This article is reproduced from serverfault.com, please click

Is there a to stop os.rename from moving a file?

发布于 2020-11-29 02:13:03

I created a script that runs perfectly fine in visual studio code but I'm now trying to automate the script, which is proving to be a little tricky. I've turned the file into a Unix executable file for the automation but when I click on my script, the code that I’ve implemented doesn’t do what I want it to.

I’ve got a line of code that changes the name of all of the .csv files within a directory.

This is the particular line of code that I was talking about…

spath='/Users/emmanuel/Documents/Selenium/'
sourcefiles = os.listdir(spath)
for file in sourcefiles:
    if file.endswith('.csv'):
        os.rename(os.path.join(spath,file), "CC.csv")

The name that I want to change the .csv file to is CC.csv and I use this coding method as the name for the .csv file has a different name every-time that it's downloaded.

The issue I'm having now is that, when the script reaches this part of the code, the .csv file just disappears from the directory and is moved from the source directory but I want to prevent that from happening. Reason being that I need the file to stay in there in order to be further manipulated by the pandas module.

Is there a way to rename all of the .cvs files in a directory and keep them in the source directory after being renamed?

I’ve run this code in VSC and it runs perfectly fine but when I run it as a unit executable file I get this error…

Traceback (most recent call last):
  File "/Users/emmanuel/Documents/Selenium/OptionsBotCode.py", line 120, in <module>
    CC = pd.read_csv('/Users/emmanuel/Documents/Selenium/CC.csv')
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 688, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 454, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 948, in __init__
    self._make_engine(self.engine)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 1180, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 2010, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas/_libs/parsers.pyx", line 674, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] No such file or directory: '/Users/emmanuel/Documents/Selenium/CC.csv'
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

I’ve already tried using

print(os.getcwd())

And I’ve been able to find where the file supposedly travels to but when I look in the directory the file doesn’t appear to be there.

To put things into context, I need the ‘pandas’ module to access the file (in order to manipulate it), but I need to either change the file name according to the file type (.csv) or have pandas access the file type as opposed to the file name.

This is because, every-time this .csv file is downloaded, it has a different name, and in order to use the

pandas.read_csv 

function, the bot would need the file to have the same name every time so it could repeat the process.

I won’t have any issue with changing multiple file names because, at the start of my script, there is a piece of code that clears out all existing .csv files in the directory so it will be only one file that is targeted. I now understand that rename does more than just rename. If the source and destination are both in the same folder, it will move the renamed file into a new directory. However, I want to keep the file in the same directory

Questioner
user14425857
Viewed
0
tdelaney 2020-11-29 10:49:53

To save the CSV in the source directory, add that directory to the destination name.

spath='/Users/emmanuel/Documents/Selenium/'
dcsv = os.path.join(spath, "CC.csv")
sourcefiles = os.listdir(spath)
for file in sourcefiles:
    if file.endswith('.csv'):
        os.rename(os.path.join(spath,file), dcsv)

Note that if there are multiple CSV's in the directory, all but the last one renamed will be deleted. Also, if an old CC.csv is still in that directory when you run this code, it may end up being the new CC.csv instead of the file you downloaded.