英文:
Directory name error when creating dir in python out of the blue
问题
我使用了我自己制作的方法来保存一些数据的图表,并通过将这些图表保存在当前工作目录的子文件夹中,该子文件夹的名称是数据的起始日期和结束日期来自动化这个过程。
我一直正常使用该函数,除了一个文件夹(只有一个文件夹)中的数据,它给出了一个与创建的目录名称有关的错误,错误信息如下:
OSError: [WinError 123] 文件名、目录名或卷标语法不正确:'C:\Users\tiger\Documents\Radiator_Test\valve_3_5\12.08.2022_21:00-12.12.2022_04:00_dir'
用于创建目录的字符串如下:
C:\Users\tiger\Documents\Radiator_Test\valve_3_5\12.08.2022_21:00-12.12.2022_04:00_dir
我过去已经成功创建过具有相同名称的文件,同时在其他数据文件夹上,我使用相同名称格式的方法正常工作。
负责创建目录的代码部分如下:
first_date, last_date = hfc.help_func.first_last_date(data)
directory = os.getcwd()
directory = os.path.join(directory, first_date + '-' + last_date + '_dir')
isExist = os.path.exists(directory)
subdir = first_date + '-' + last_date + '_dir'
if not isExist:
# 因为它不存在,所以创建一个新的目录
os.makedirs(directory)
我已经尝试查看其他我使用并正常工作的方法,它们与这个不工作的方法完全相同。请注意,它过去运行正常,而现在在不更改代码的情况下出现问题。
如果需要更多细节或代码的其他部分,请告诉我,我会提供它们。
英文:
So i am using a method i made to save some plots of data, and i have automated the process by saving the plots in a sub folder in my current working directory, that has the name of the first and last dates of the data i am doing the plots for.
I have been using said function normally with no problem except one folder (yes only one) with data, gives me an error related to the name of the directory being created as it being incorrect
> OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\Users\tiger\Documents\Radiator_Test\valve_3_5\12.08.2022_21:00-12.12.2022_04:00_dir'
the string created to be used to make the dir is this:
> C:\Users\tiger\Documents\Radiator_Test\valve_3_5\12.08.2022_21:00-12.12.2022_04:00_dir
which i have already in the past made successfully files that have this name already and at the same time on other data folders i use the same method with the same format of the name and it works normally
the part of the code that is responsible for the directory creation is this:
first_date, last_date = hfc.help_func.first_last_date(data)
directory = os.getcwd()
directory = os.path.join(directory,first_date + '-' + last_date + '_' + 'dir')
isExist = os.path.exists(directory)
subdir = first_date + '-' + last_date + '_' + 'dir'
if not isExist:
# Create a new directory because it does not exist
os.makedirs(directory)
I have tried looking at other methods i use and work normally and they identical to this one that doesn't work. Mind you it used to work fine and randomly without changing that part of the code it doesn't
If more details are required or parts of the code please let me know I will provide them
答案1
得分: 1
如果您正在使用Windows,那么在目录名称中使用:
是无效的,例如,当手动重命名一个包含:
的文件夹时,您将会遇到问题:
因此,建议考虑将:
的值简单替换为_
或-
,就像您一直在做的那样。
只需使用以下代码即可:
first_date, last_date = [x.replace(":", "") for x in hfc.help_func.first_last_date(data)]
英文:
If you are using windows, then using :
as part of the directory name is not valid, for example, when manually renaming a folder with :
you would get:
Therefore, simply consider replacing the :
values for _
or -
as you have been regularly doing.
Simply using this should suffice:
first_date, last_date = [x.replace(":","") for x in hfc.help_func.first_last_date(data)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论