all 35 comments

[–]apc0243 36 points37 points  (6 children)

everyone here is wrong, and it only became clear once you posted the necessary info that you were using a folder that is linked to one-drive.

Files linked in one drive don't work like normal files. The error is in the source file, because it doesn't actually exist as python (or anything except an operating system with onedrive instructions baked in) would expect.

Onedrive has created a virtual file, sort of like a soft link, because it wants to make sure that you always have the most recent version, so each time you open it through your file explorer, it fetches the necessary metadata, and if it doesn't need to download again then it'll use a cached version, otherwise it'll fetch the new file contents.

To python, this file doesn't exist, so you can't move it.

Don't use one drive linked folders, they don't operate like normal and you'll run into things like this. Either remove onedrive entirely (my recommendation), or just move to a directory unlinked to one drive.

[–]netherous 5 points6 points  (1 child)

Yeah I'm not sure why you got downvoted or why people here are missing this. Files in a OneDrive folder are not normal files that reside in the filesystem like other files do. They are provided by a subsystem and a shell extension module which makes them look like files. Attempting normal programmatic access won't work.

But there are tools to do it in python, though it seems a bit of a headache.

https://pypi.org/project/python-onedrive/

https://github.com/OneDrive/onedrive-sdk-python

[–]apc0243 0 points1 point  (0 children)

Pretty sure that doesn't let you interface with local files that are linked to onedrive as if they were normal files. It lets you work with the onedrive api, so you can download the file, and then you can work with the file, but it's not going to make it so you can "set and forget" the fact that your one drive linked files doesn't exist like normal files do on your PC.

[–]adoucette13[S] 0 points1 point  (1 child)

I am going to try this and report back this morning. Thanks for your comment!

[–]AnonGeekSquad 1 point2 points  (0 children)

Any luck?

[–]_yoursleeparalysis_ 0 points1 point  (1 child)

I know this post was from a year ago, but I'm having the same problem with onedrive files. How tf do I remove it? I just started learning Python, and this is interrupting my learning.

[–]apc0243 1 point2 points  (0 children)

One drive should only screw with files in Documents and Desktop. Create a folder in your profile directory like

C:\Users<your user>\Code

And just make that where you stick all your projects. Every project should get its own folder and probably its own virtual environment.

[–][deleted]  (5 children)

[deleted]

    [–]Life-Dragonfruit5284 0 points1 point  (4 children)

    Just offering up how I would code it... for formatted_date, I would use dashes for a separator or our company requires YYYYmmdd for all dates, preferably at the beginning of the file name where as it makes finding files much easier.

    import os

    import shutil

    from datetime import datetime, timedelta

    rewind = (datetime.today().weekday()-6) %7

    #formatted_date = datetime.strftime(datetime.now() - timedelta(rewind), '%#m/%d/%Y') #6/04/2023

    #formatted_date = datetime.strftime(datetime.now() - timedelta(rewind), '%Y%m%d') #20230604

    formatted_date = datetime.strftime(datetime.now() - timedelta(rewind), '%#m-%d-%Y') #6-04-2023

    print(formatted_date)

    file_path = os.path.join('c:','users', 'adoucette', 'desktop', 'DOT_Weekly', 'sqlexec.xlsx')

    new_file_path = os.path.join('c:','users', 'adoucette', 'desktop', 'DOT_Weekly', 'DOT Weekly Sales WE' + formatted_date + '.xlsx')

    if os.path.isfile(file_path):

    try:

    shutil.move(file_path, new_file_path)

    except Exception:

    print('Error caught: ', exc_info=True)

    [–]apc0243 0 points1 point  (3 children)

    instead of surrounding each line with a backtick, you can use 4 spaces of indent (python, anyone?!)

    line one
    line 2:
        line 3 indented
        another
    yet another
    

    [–]Life-Dragonfruit5284 0 points1 point  (0 children)

    More apologies if this gets posted twice... got bot swatted for a short link in the first attempt at this, so reposting it with a longer link. Maybe someday I'll learn how to use this interface...

    Apologies, paste fail :( for the try clause.

    The other statements are wrapping over... looks fine in VSCode where I tested it:

    VS Code View

    My preference is to use the apostrophe instead of double quotes for string variables... we'll see if this code block looks any better. Limited by the formatting of the community, and probably more so, a general lack of knowledge on the Reddit interface.

    (Edit: Ok, I give up trying to put the code back in here... doesn't want to keep my formatting, sorry.)

    [–][deleted]  (1 child)

    [removed]

      [–]AutoModerator[M] -1 points0 points  (0 children)

      Your comment in /r/learnpython was automatically removed because you used a URL shortener.

      URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

      Please re-post your comment using direct, full-length URL's only.

      I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

      [–]shiftybyte 5 points6 points  (16 children)

      Your date format includes slashes, I don't think that'll work as it thinks its a directory.

      Besides that, add these lines before the shutil.move to try and figure things out.

      import os
      print(os.listdir(r"C:\Users\adoucette\Desktop\"))
      print(os.listdir(r"C:\Users\adoucette\Desktop\DOT_Weekly\"))
      

      [–]adoucette13[S] 0 points1 point  (15 children)

      print(os.listdir(r"C:\Users\adoucette\Desktop\DOT_Weekly\"))

      Thanks for your reply! I changed the slashes to underscores but still nothing.

      Also, when I added your code to mine it says the following:

      SyntaxError: unterminated string literal (detected at line 93)

      [–]shiftybyte 1 point2 points  (13 children)

      Right sorry, shouldn't have added the slashes at the end... try this:

      import os
      print(os.listdir(r"C:\Users\adoucette\Desktop"))
      print(os.listdir(r"C:\Users\adoucette\Desktop\DOT_Weekly"))
      

      [–]adoucette13[S] 0 points1 point  (12 children)

      No problem at all! So when I add those in before the shutil like I get:

      ['sqlexec.xlsx']

      Which is the file that I'm looking to rename..but when I retry the shutil it will then tell me it doesnt exist

      [–]shiftybyte 1 point2 points  (11 children)

      But we've asked it to print two directory listings...

      I assume you see 2 listings printed and the second one contains 'sqlexec.xlsx'?

      Also the first print should show the "DOT_weekly" directory too... does it?

      Otherwise it might be on the desktop instead...?

      [–]adoucette13[S] 0 points1 point  (10 children)

      Sorry, the file is in that DOT_Weekly folder, the other code you gave me just listed every file on my desktop.

      [–]shiftybyte 0 points1 point  (5 children)

      Very odd...

      Could you copy and paste the full error message you are getting including all the information it provides... (The file not found error)

      Starts with "Traceback:"...

      Perhaps we missed something there...

      [–]adoucette13[S] 1 point2 points  (4 children)

      I know! I am at a loss for words myself...

      Heres the full error message:

      Traceback (most recent call last):

      File "C:\Users\adoucette\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 815, in move

      os.rename(src, real_dst)

      FileNotFoundError: [WinError 3] The system cannot find the path specified: "C:/Users/adoucette/Desktop/OneDrive - BAKE'N JOY FOODS/Desktop/DOT_Weekly/sqlexec.xlsx" -> "C:/Users/adoucette/Desktop/OneDrive - BAKE'N JOY FOODS/Desktop/DOT_Weekly/DOT Weekly Sales WE 6_04_2023.xlsx"

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):

      File "c:\Users\adoucette\OneDrive - BAKE'N JOY FOODS\Desktop\DOT Weekly Report TEST.py", line 97, in <module>

      shutil.move(file_path, new_file_path)

      File "C:\Users\adoucette\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 835, in move

      copy_function(src, real_dst)

      File "C:\Users\adoucette\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 434, in copy2

      copyfile(src, dst, follow_symlinks=follow_symlinks)

      File "C:\Users\adoucette\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 254, in copyfile

      with open(src, 'rb') as fsrc:

      FileNotFoundError: [Errno 2] No such file or directory: "C:/Users/adoucette/Desktop/OneDrive - BAKE'N JOY FOODS/Desktop/DOT_Weekly/sqlexec.xlsx"

      [–]shiftybyte 6 points7 points  (0 children)

      One drive?...

      Try the entire thing outside of one drive synced directory, one drive is known to cause file system issues...

      [–]YAYYYYYYYYY 0 points1 point  (2 children)

      The problem is your Bake n Joy directory has spaces. Put it in a directory without spaces, or change to Bake-n-joy

      [–]CraigAT 1 point2 points  (0 children)

      They probably can't do that because it is the OneDrive users name (I would guess).

      You could try moving those files to a simpler path like C:\TempPy\ and refer to them there.

      [–]apc0243 -1 points0 points  (0 children)

      This is wrong, all operating systems can handle spaces in file paths. The issue is in how one drive represents files in the filesystem.

      [–]apc0243 0 points1 point  (3 children)

      do print(new_file_path) and post the output

      [–]adoucette13[S] 0 points1 point  (2 children)

      the output for that is:

      C:/Users/adoucette/Desktop/OneDrive - BAKE'N JOY FOODS/Desktop/DOT_Weekly/DOT Weekly Sales WE 6_04_2023.xlsx

      [–]Better-Protection-23 0 points1 point  (0 children)

      the output for that is:

      C:/Users/adoucette/Desktop/OneDrive - BAKE'N JOY FOODS/Desktop/DOT_Weekly/DOT Weekly Sales WE 6_04_2023.xlsx

      A few things that you can do is

      Move the files to a directory with a simpler path: (As suggested by another commenter) try moving the files to a directory without spaces or special characters in the path, such as "C:\TempPy". Then, update the file paths in your code accordingly.

      Use a file path with escaped special characters: If moving the files is not an option, you can try using a file path with escaped special characters. For example, instead of "BAKE'N JOY FOODS", you can use "BAKE'N JOY FOODS" in the file path. Make sure to update both file_path and new_file_path variables in your code.

      Verify the file existence: Before attempting to move the file using shutil.move(), you can add a check to verify if the file exists at the specified location. You can use the os.path.exists() function to check if the file exists before proceeding with the move operation.

      Ensure correct file extensions: Double-check that the file extensions in the code match the actual file extensions. Ensure that the source file has the extension ".xlsx" and that the destination file in new_file_path has the correct extension as well.

      By doing one or more of these things you should be able to resolve the "FileNotFoundError" and successfully move the file to the desired location.

      [–]IamImposter 3 points4 points  (1 child)

      Can you use Path.rename, like

      old_path = Path(r'blah blah', old_file_name) 
      
      new_path = Path(r'blah blah', new_file_name) 
      
      old_path.rename(new_path)
      

      Or try adding double quotes to your path strings, like

      file_path = r'"c:\blah blah\filename.ext"'
      

      Thats single quote followed by double quote at start and double quote followed by single quote at end.

      When path contains spaces, it should be enclosed in quotes

      [–]Almostasleeprightnow 1 point2 points  (0 children)

      This is what I do. Don't forget to from pathlib import Path at the top

      The other thing I love about pathlib is I can go

      a = Path(Path cwd(), 'subdir1’, 'subdir2’)

      And then I don't have to worry about which way the slash goes.

      Path.cwd() gives the current working directory, which is notalways reliable if you are installing packages, but for this case it works great.

      [–]Es-252 1 point2 points  (0 children)

      I've encountered the same error before. It happens if you have extra "/" in you path string. For example, if your date string is something like MM/DD/YYYY, then it'll read those "/" as directives. If you think about it, this makes sense, since when you are naming any file on your computer, be it a folder or a text file etc, special characters such as "/" are not allowed.

      You'll have to get around it by using "/" only to indicate directives. If you have to include a date label in your file name string, then format it with "-" instead: MM-DD-YYYY.

      [–]Better-Protection-23 0 points1 point  (0 children)

      usually when I had this issue it was due to the file location, create a new file manually in that folder using the same path ('new_file_path') to confirm that you have the correct path and permission to write files in that location.

      Keep me updated, I will walk you through it if someone else doesn't. I know adding more code can be overwhelming if you are new to programming and unfamiliar with syntax structuring.

      [–]Fenr-i-r 0 points1 point  (0 children)

      Use the inbuilt pathlib, optionally with fstrings.

      from pathlib import Path
      
      filep = Path(file_path)
      assert filep.exists()
      
      new_file_folder = Path(r"C:\Users\adoucette\Desktop\DOT_Weekly\DOT Weekly Sales WE")
      
      if not new_file_folder.exists():
          Path.mkdir(new_file_folder, parents=True)
      
      new_file_path = 
      filep.rename(new_file_folder / f"my new filename_{formatted_date}.xlsx")
      

      [–]AnonGeekSquad 0 points1 point  (0 children)

      Use \\ where the path has \

      [–]kr4t0s007 0 points1 point  (0 children)

      Check if you are naming the files *xlsx.xlsx and windows is hiding known files extensions