简体   繁体   English

无法创建目录和子目录

[英]Unable to create directories and subdirectories

So, I'm trying to make folders and subfolders based on date variables.所以,我试图根据日期变量制作文件夹和子文件夹。 I am getting this error.我收到此错误。 Can someone assist?有人可以帮忙吗? The code and the error message is as shown below.代码和错误信息如下所示。

for date in date_list:
    year = date.year
    month = date.month
    day = date.day
    newpath = f'C:\Users\divya\OneDrive\Desktop\8th Sem\ISB assignment\{year}'
    if not os.path.exists(newpath):
        os.makedirs(newpath)
    newpath1 = f'C:\Users\divya\OneDrive\Desktop\8th Sem\ISB assignment\{year}\{month}'
    if not os.path.exists(newpath1):
        os.makedirs(newpath1)
    newpath2 = f'C:\Users\divya\OneDrive\Desktop\8th Sem\ISB assignment\{year}\{month}\{day}'
    if not os.path.exists(newpath2):
        os.makedirs(newpath2)
    get_batsmen(date).to_csv(newpath2+'/batsmen.csv')
    get_bowler(date).to_csv(newpath2+'/bowler.csv')
    get_allrounder(date).to_csv(newpath2+'/allrounder.csv')

And the error message is as follows:错误信息如下:

File "c:\Users\divya\OneDrive\Desktop\8th Sem\ISB assignment\main.py", line 67
    newpath = f'C:\Users\divya\OneDrive\Desktop\8th Sem\ISB assignment\{year}'
             ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

The backlash is the escape character in Python, so when python sees \U it interprets it as the start of a unicode character (which takes the form \UXXXXXXXX , hence the error text), and not a regular "backlash U".反斜杠是 Python 中的转义字符,因此当 python 看到\U时,它会将其解释为 unicode 字符的开头,因此是 U" 字符(其形式为\UXXXXXXXX常规“XXX”。错误)

You can get a 'real' backslash by putting another backslash before it, so the full string becomes something like "C:\\Users\\username\\path\\to\\directory" .您可以通过在其前面放置另一个反斜杠来获得“真正的”反斜杠,因此完整的字符串变为类似"C:\\Users\\username\\path\\to\\directory"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM